123hal321
123hal321

Reputation: 2090

How to change scenes in cocos2d while retaining state of the original scene

In my cocos2d project, I have two scenes. I transition between the two using CCDirector's replaceScene. Is it possible to save the state of the current scene so that when the scene is changed to a different scene, and then changed back to the original, all the objects and variables in the original are the same.

Thank you,

nonono

Upvotes: 4

Views: 6165

Answers (1)

Colin Gislason
Colin Gislason

Reputation: 5589

Instead of using replaceScene, you can use pushScene: and popScene. pushScene: puts the new scene onto a stack and displays it. When you are done that scene, call popScene to return to the previous scene on the stack.

[[Director sharedDirector] pushScene: newScene];
//...
[[Director sharedDirector] popScene];

Note that this does leave your previous scene in memory (as you asked), so it is recommended to use replaceScene: if you possibly can. If you do use pushScene: and popScene, it is best to keep your scene stack pretty small.

Upvotes: 9

Related Questions