cocos2d-iphonecclayer

Cocos2d - running a scene a second time doesn't schedule updates


From a UIKit ViewController I do this to start a game:

if (director.runningScene == nil)
    {
        [director runWithScene:[MyGameLayer scene]];
    }
    [director startAnimation];

MyGameLayer looks like this:

+(id) scene {
    CCScene *scene = [CCScene node];
    MyGameLayer* layer = [MyGameLayer node];
    [scene addChild:layer z:0 tag:GameSceneLayerTagGame];


    return scene;
}

-(id) init {
    if ((self=[super init]))
    {
        NSLog(@"init!");
        self.isTouchEnabled = YES;

        [self initPhysics];

        [self scheduleUpdate];
    }
    return self;
}

The first time I play the game, everything works fine. -(void) update: (ccTime) dt gets called as it should. If I exit the game and do this:

CCDirectorIOS* director = (CCDirectorIOS*)[CCDirector sharedDirector];
    [director stopAnimation];
    [director end];

And then play the game again, -(void) update: (ccTime) dtdoes not get called, even though I know -init() is getting called. The game appears on the screen, but the update scheduling doesn't work the 2nd time around. What would cause Cocos2d to not schedule the update and how do I fix it?


Solution

  • Finally figured this out. When opening the menu in the game which has the quit button, I was pausing the game like this:

    [[self gameLayer] pause];
    

    So when I would start a new game, Cocos2d was still paused, even though the scene and layers were deallocated and I was creating a new glView.

    If I use this it works:

    [[self gameLayer] pauseSchedulerAndActions];