// TRY TO AVOID A BIG STACK OF PUSHED SCENES
-(void) mainMenu()
{
// etc
[[CCDirector sharedDirector] pushScene: gameScene];
}
// stack:
// . game <-- running scene
// . mainMenu
-(void) game
{
[[CCDirector sharedDirector] pushScene: gameOverScene];
}
// stack:
// . gameOver <-- running scene
// . game
// . mainMenu
-(void) showGameOver
{
[[CCDirector sharedDirector] pushScene: hiScoreScene];
}
// stack:
// . scores <-- running scene (4 pushed scenes... expensive)
// . gameOver
// . game
// . mainMenu
-(void) toMainMenu
{
[CCDirector sharedDirector] replaceScene: mainScene];
}
// stack:
// I don't know...
I want to kill all the scenes in the stack after replaceScene: call. But I don’t know if it happens.
Just use replaceScene throughout.
Pushing & popping scenes only makes sense for quick intermissions, ie showing the settings screen. You will want to avoid any scenario where you push a scene, where it might potentially be followed by a call to replaceScene. It gets very complicated and inconvenient to keep track of the current scene stack (Director exposes no convenience methods to let you manage the scene stack). It’s very easy to accidentally keep growing the stack of pushed scenes until you run out of memory.
The short answer is: avoid popping and pushing scenes whenever possible. If you need to keep state, create a Singleton class that stores the values that need to be persisted when replacing scenes.