Pause scene with cocos2d an SpriteBuilder

Often games need something like a pause scene to pause the current game and to provide further options like quitting or restarting the current game. So as you may think this isn’t a big deal, right?

Well the simples approach may be the following:
1) Create the game scene.
2) Whenever a user decides to pause the game load a pause scene and push it to the CCDirector.
3) If the user decides to resume the game simple pop the current scene.

Well, now what will you do if the user decides to quit the game and go back to the main menu for instance? Loading the main menu scene and replace the current scene is not the way to go here, because the game scene is still on the scene stack. In this case you are responsible to cleanup the whole scene stack on your own. Simply pop the scene multiple times often generates flickering, which you want to avoid in any case.

Since I don’t want to manage the scene stack on my own, I try to avoid this pitfall in any case. To implement a pause screen I use the following approach:

1) Within SpriteBuilder I create a new file PauseNode, setup my pause screen and connect the root CCNode with anowner variable i.e. pauseNode.
2) I also setup my actions (like resume, quit, etc.) and connect it with selector of the Owner target.
3) In my actual game scene, I add a new instance variable CCNode *pauseNode.
4) When loading the game scene, I also load the pause node with [CCBReader load:@"PauseNode" owner:self];. This can be done inside the onEnter method.
5) Whenever the user pauses the current game, I simply unschedule all timers and present the pause node with [self addChild:pauseNode];.
6) If the user decides to resume the current game I remove the node and restart my timers.
7) However, if the user decides to quit the game, I can simply replace the current scene without having any worries about the scene stack at all.

That’s it! Hope it helps!

Leave a Reply

Your email address will not be published. Required fields are marked *