iosobjective-ccocos2d-iphonekobold2d

+(id) scene not called after replaceScene


I have a menu item which will call goToGameLayer when tapped. The goToGameLayer method simply does the following:

[[CCDirector sharedDirector] replaceScene:(CCScene*)[[GameLayer alloc] init]];

However, after replaceScene, the +(id)scene method in GameLayer is not called

+(id)scene{
    NSLog(@"+(id)scene CALLED");

    CCScene *scene = [CCScene node];
    GameLayer *layer = [GameLayer node];
    [scene addChild: layer];
    return scene;
}

I find that dealloc method in the menuLayer is called after the init method in GameLayer, but I'm not sure whether that interrupts the dispose of current scene.

Thanks for any suggestion!


Solution

  • The scene method is not being called because you initialize the object using:

    [[GameLayer alloc] init]];
    

    If you use

    [GameLayer scene];
    

    instead the method will be called and you can also omit the cast to CCScene:

    [[CCDirector sharedDirector] replaceScene:[GameLayer scene]];