0

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?

Sebastian
  • 7,670
  • 5
  • 38
  • 50
soleil
  • 12,133
  • 33
  • 112
  • 183

4 Answers4

0

Did you override (implement) one of the onEnter*/onExit* methods? If so, make sure you call the super implementation otherwise scheduling and touch input may not work.

Update: Other than that try not calling [director end] because it shuts down cocos2d completely which is known to be problematic.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • If I don't call [director end], the 2nd time I start the game I just get a pink screen. Followed by an endless stream of openGL errors: OpenGL error 0x0501 in -[CCSprite draw] 532 – soleil Mar 14 '13 at 18:10
0

Assuming that you are doing what you are doing to prevent the director from updating with the 4 fps when working in background, you should maybe try to start the director again.

That being said, in your applicationDidBecomeActive, add the following code

[[CCDirector sharedDirector] stopAnimation]; // call this to make sure you don't start a second display link!
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];

Taken from: How to implement pause/resume in cocos2d game?

If I am incorrect please let me know what you are trying to do with your director calls

Community
  • 1
  • 1
Christian
  • 11
  • 1
  • I have a view controller that has multiple mini games to choose from. So I need to be able to quit one game and start another. The problem is that the update scheduler only works on the first game I play. No animation happens on any subsequent plays of the same game or any other. – soleil Mar 14 '13 at 05:04
0

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];
soleil
  • 12,133
  • 33
  • 112
  • 183
0

When you 'pause' your game by using this code:

[[CCDirector sharedDirector]pause];

You also have to 'resume' your director in the restart or menu code block before pushing any scene:

[[CCDirector sharedDirector]resume]; 
n.by.n
  • 2,458
  • 22
  • 31