获取当前场景返回NULL?

时间:2012-02-23 22:06:40

标签: objective-c cocos2d-iphone

我正在尝试重新加载当前场景,然后init发生dealloc

我希望在dealloc上有一个条件,如果它从当前场景调用,则返回。

我正在尝试这个:

if ([[[CCDirector sharedDirector] runningScene] getChildByTag:16]) 
return;

[[[CCDirector sharedDirector] runningScene] isKindOfClass:[HelloWorldLayer class]]

两者都给我NULL,条件不正确。

为什么是???

我的场景如下:

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
     CCScene *gameScene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];

    layer.tag=16;
    // add layer as a child to scene
    [gameScene addChild: layer];

    // return the scene
    return gameScene;
}

当我只刷新场景时,如何消除dealloc?

1 个答案:

答案 0 :(得分:1)

永远不要这样做:

[[CCDirector sharedDirector] replaceScene:self]

或者这个:

CCDirector* director = [CCDirector sharedDirector];
[director replaceScene:director.runningScene];

只需将self或当前运行的场景传递给replaceScene方法,就无法替换当前场景。您始终必须创建CCScene类的新实例:

[[CCDirector sharedDirector] replaceScene:[MyScene scene]]

小心runningScene。第一次将runWithScene消息发送给director时,runningScene将始终为nil。您可以将某些初始化推迟到onEnter方法:

-(void) onEnter
{
   // runningscene is valid in onEnter
   [[CCDirector sharedDirector] runningScene];
   [super onEnter];
}