Cocos2d - 使用getChildByTag从子类获取场景中的对象 [英] Cocos2d - using getChildByTag from child classes to get objects in a scene

查看:2686
本文介绍了Cocos2d - 使用getChildByTag从子类获取场景中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个cocos2d场景,有几个子对象组成屏幕显示。这些子对象中的一些需要彼此通信,以便可以更新显示。

I have a cocos2d scene that has several child objects that make up the screen display. Some of these child objects need to communicate with each other so that the display can be updated.

我决定不是传递对场景图中对象的引用,标记所有的cocos2d节点,传递标签值,然后每当我需要一个场景对象,我只需使用director使用标签检索对象。

I decided that instead of passing around references to objects in the scene graph I would tag all the cocos2d nodes, pass the tag values around and then whenever I need a scene object I would just use the director to retrieve the object using the tag.

这意味着我没有很多引用对象飞来飞去,我的想法是,它会更清洁,不太可能导致内存问题,

This means I don't have lots of references to objects flying around and my thinking was that it would be cleaner and less likely to cause memory problems with objects being retained when they shouldn't be.

我使用以下代码检索特定节点:

I use the following code to retrieve a particular node:

CCNode* node = [[[[CCDirector sharedDirector] runningScene] getChildByTag:TAG_MY_LAYER] getChildByTag:TAG_MY_OBJECT];
if (node != nil ){
    NSAssert([node isKindOfClass:[myObject class]], @"node is not a myObject");
    myObject* mo = (myObject*)node;
    ...
    other stuff
    ....

问题:当场景使用转换初始化时,此方法不起作用。对运行场景的调用返回过渡而不是新场景。如果你等待 - onEnterTransitionDidFinish然后它仍然不工作。看起来好像你必须等待一段时间后(推测旧的场景被销毁),在转换被移除之前,runningScene是你的新场景。

Problem: When a scene is initialising with a transition then this method doesn't work. The call to running scene returns the transition and not the new scene. If you wait for the - onEnterTransitionDidFinish then it still doesn't work. It seems as if you have to wait for a little bit after this (presumably for the old scene to be destroyed) before the transition is removed and runningScene is your new scene.

是否可以获得对场景图的一部分的引用,我可以调用getChildByTag,它将获得我的标记对象,而不管场景的转换状态?

Is it possible to get a reference to part of the scene graph that I can call getChildByTag from and it will get my tagged object, regardless of the state of scenes transitioning?

推荐答案

@erik - 你的答案与问题有关吗?

@erik - how is your answer relevant to the question asked ?

您可以将代码移动到onEnter()方法,该方法似乎在转换到新场景之后执行,即:

You can move your code to the onEnter() method which seems to be executed after transition to the new scene is completed ie :

-(void)onEnter {
   [super onEnter];

   CCNode* node = [[[[CCDirector sharedDirector] runningScene] getChildByTag:TAG_MY_LAYER] getChildByTag:TAG_MY_OBJECT];
   if (node != nil ){
     NSAssert([node isKindOfClass:[myObject class]], @"node is not a myObject");
     myObject* mo = (myObject*)node;
     ...
     other stuff
     ....
   }
}

这篇关于Cocos2d - 使用getChildByTag从子类获取场景中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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