Cocos2d:我应该只使用 (id)init 进行初始化,还是可以使用 (void)onEnter 进行后期分配?

发布于 2024-12-02 13:32:40 字数 506 浏览 0 评论 0原文

在学习Cocos2d的过程中,我发现- (void)onEnter- (void)onExit可以像它们的对应项一样使用,- (void)viewDidLoad< /code>, - iOS UIKit 的 (void)viewDidUnload

我确实知道它们在技术上并不能很好地替代原始的 - (id)init,而且我经常看到学习 Cocos2d 的指导性文本仅使用 - (id)init 方法它的所有属性和变量。

但是,如果我使用 - (void)onEnter 进行分配和初始化,我可以引用 self.parent 以获得更简洁的代码。

可以安全地假设我可以继续使用 - (void)onEnter 进行分配和初始化,而不用担心这是否是使用 Cocos2d 框架进行开发的正确方法?

As learning Cocos2d, I've found - (void)onEnter, - (void)onExit, can be used like their counterparts, - (void)viewDidLoad, - (void)viewDidUnload of iOS UIKit.

I do aware they are not technically good replacement for the original - (id)init, and I often see instructive texts for learning Cocos2d only use - (id)init method for all it's properties and variables.

However, if I use - (void)onEnter for allocation and initialization, I can reference self.parent for a little more cleaner code.

Is it safe to assume I can keep using - (void)onEnter for allocating and initializing without worrying if it's right way to develop using Cocos2d framework?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤星 2024-12-09 13:32:40

假设您对初始化和内存管理非常小心,这听起来是一种完全合理的处理代码的方法,但正如您所提到的,它不是标准的,因此请注意一些陷阱。

-(void) onEnter-(void) onExit 方法实际上更像 -(void) viewWillAppear-(void ) viewWillDisappear 优于 -(void) viewDidLoad-(void) viewDidUnload

请注意:

  • onEnter 也会在节点的所有子节点上递归调用。
  • onEnter 可能会被调用几次,具体取决于您处理场景的方式,而 init 只会被调用
    一次。小心你的对象不会因意外分配而泄漏
    他们不止一次。

Assuming that you are careful about your initializations and memory management, this sounds like a perfectly reasonable way to handle your code but as you mention, it's not standard so please beware of some pitfalls.

The -(void) onEnter and -(void) onExit methods are actually more like -(void) viewWillAppear and -(void) viewWillDisappear than -(void) viewDidLoad and -(void) viewDidUnload

Be aware of:

  • onEnter is also called recursively on all of your node's children.
  • onEnter may be called several times depending on how you handle your scenes whereas init would only be called
    once. Be careful your objects don't leak by accidentally allocating
    them more than once.
相思碎 2024-12-09 13:32:40

这是可行的,但却是一个糟糕的方法。 onEnter 在 CCNode 添加到另一个 CCNode 时调用,而不是在它或其父级添加到屏幕时调用。 onEnter 和 onEnterTransitionIsFinished 表示 CCNode 已加载并准备就绪。除非您特意将 CCNode 保留在另一个 CCNode 之外(不好的形式),否则 onEnter 将在 init 之后立即调用。所以你所谓的惰性分配是没有意义的。

更重要的是,无论如何,您应该在 CCScene 开始时加载尽可能多的可缓存数据(纹理等),以防止场景中出现问题,例如延迟分配大量可缓存数据。如果您在场景开始时正确完成了所有繁重的工作,那么等待设置一些标志和数字变量不会给您带来任何好处。如果用一种适用于其他事情的方法来实现这一点,那就更无济于事了。

It's doable, but a bad approach. onEnter is called when the CCNode is added to another CCNode, not when it or its parent is added to the screen. onEnter and onEnterTransitionIsFinished is meant to represent that the CCNode is loaded and ready to go. And unless you specifically keep your CCNodes outside of another CCNode (bad form), onEnter will be called immediately in the after your init. So your supposed lazy allocation is moot.

More importantly, you should be loading as much cacheable data (textures and such) at the start of your CCScene anyway, to prevent in-scene hiccups such as lazy allocation of large, and therefore, cacheable data. And if you've correctly done all that heavy lifting at the start of your scene, waiting to set some flags and number variables isn't going to do you any good. And doing so in a method that's meant for other things is even less helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文