Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 699
123hal321 Avatar asked Jan 21 '23 16:01

123hal321


1 Answers

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.

like image 123
Colin Gislason Avatar answered Apr 09 '23 03:04

Colin Gislason