在iPhone的Cocos2D游戏编程教科书的循环引用 [英] Circular references in iPhone Cocos2D game programming textbook

查看:95
本文介绍了在iPhone的Cocos2D游戏编程教科书的循环引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个滥用的主题,但我找不到这个答案。我下面的学习iPhone和iPad cocos2d游戏开发书,不能真正了解如果ShootEmUp示例中的方法(可在 1 )是最好的。作者使用GameScene,作为子对象添加各种对象(例如Ship,InputLayer等..)。有争议的方面是,在这些对象内有通过使用一个静态方法返回一个静态实例GameScene类,在GameScene的init方法实例化的GameScene调用。这对我来说似乎是一个循环引用,根据许多(例如,看到这篇文章)是需要避免的。我不太确定如果在游戏编程中也是如此,因为这种方法可以在 1 ,可能有原因。

an abused topic but I couldn't find an answer to this. I am following the "Learn iPhone and iPad cocos2d Game Development" book and cannot really understand if the approach in the ShootEmUp example (available at 1) is the best one. The author uses a GameScene to which adds as child various objects (e.g. Ship, InputLayer etc..). The controversial aspect is that within those object there are calls to the GameScene via using a static method that returns a static instance of the GameScene class that is instantiated in the init method of GameScene. This to me seems a circular reference and according to many (e.g. see this post) is something to avoid. I am not quiet sure if that's also true in Game Programming as this approach is found in 1 and there might be a reason for this.

任何人都能够澄清吗?我不确定是否完全重构我的代码或保持静态变量方法。

Would anyone be able to clarify? I am undecided whether to restructure my code completely or keep the static variable approach.

非常感谢:)!

源代码

推荐答案

你在这里看到的是一个半单例模式,它在Cocos中广泛使用,实际上Cocos框架本身完全建立在单例对象上(就像很多苹果的UIKit)。游戏经常使用单身,因为你通常在游戏中有很多中心数据,如分数,健康,武器等,许多你的对象需要一些知识。你也通常有对象,如玩家,敌人等,需要通知你的应用程序的中央调度他们在做什么,所以其他对象在游戏中可以做出反应或相应调整。

What you see here is a semi-singleton pattern, and it is used extensively throughout Cocos, in fact the Cocos framework itself is built entirely on singleton objects (as is a lot of Apple's UIKit). Games frequently employ singletons because you typically have a lot of central data in a game, like scores, health, weapons, etc that many of your objects need some knowledge of. You also typically have objects, like players, enemies, etc that need to notify the central dispatch of your app what they are doing so other objects in the game can react or adjust accordingly.

这就是为什么许多Cocos游戏使用你在这里展示的技术。如果你理解单例程序设计的风险,这不是坏习惯。基本上,请记住这一点:

This is why many Cocos games use the technique you've shown here. It is not bad practice if you understand the risks of singleton programming. Basically, keep this in mind:


  • 无论是使用单例模式技术还是使用另一种方法调用父类,做同样的事情,无论哪种方式。它可能更好地直接引用中央游戏引擎直接依赖的方法来为你导出。我不建议使用 [self parent] ,因为以后可能很难读取和调试,当你第一次要显示谁是父,而不是单例访问允许

  • 孩子不应该保留其父母。你可以引用父,但不保留。

  • 此处的单例方法的替代方法是在子元素中创建指向父元素的iVar。但是这本质上是相同的想法,因此为了最小化保留周期的风险,访问单例通常更安全。如果您的iVar设置不正确,您可以有一个循环引用。您在此显示的方法不是循环引用。

  • Whether you use a singleton-style technique or instead call up the parent using another method, you are essentially doing the same thing either way. It's probably better to directly reference the central game engine directly than rely on methods to derive it for you. I would not recommend using [self parent] as that can get hard to read and debug later when you first have to figure "who is the parent," instead a singleton access lets you know immediately who you are accessing.
  • A child should never retain its parent. You can reference the parent, but don't retain.
  • An alternative to the singleton approach here is to make an iVar in the child that points to the parent. But this is essentially the same idea, so to minimize the risks of a retain cycle, accessing the singleton is typically safer. If your iVar is not set properly, you could have a circular reference. The method you've shown here is not a circular reference.

请注意,使用 +(GameScene *)sharedGameScene 方法,直到之后 GameScene已初始化。这是什么使它成为一个半单例。通常,单例中的这个方法将足够聪明,以便在没有初始化的情况下初始化自己,以便使用这个类方法返回或者先创建然后返回对象。

Note that this particular code prevents you from using +(GameScene*) sharedGameScene method until after the GameScene has been initialized. This is what makes it a semi-singleton. Typically, this method in a singleton will be smart enough to initialize itself if it is not already initialized so that using this class method either returns or first creates and then returns the object.

在Cocos中可能不是一个问题,因为你可能在你做任何事情之前初始化游戏场景,因此它已经存在。

Probably not an issue in Cocos since you will likely initialize the Game Scene before you do anything else, so it will already exist.

这篇关于在iPhone的Cocos2D游戏编程教科书的循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆