Game Development Reference
In-Depth Information
The biggest issue with pushScene and popScene is that you need to keep track of
how many scenes were pushed in order to pop the exact amount. If you're not very
careful about managing your pushes and pops, you'll end up forgetting a pop or pop-
ping one scene too many. Besides, all those scenes have to share the same memory.
There are cases where pushScene and popScene are very handy. One is if you
have one common scene used in many places, such as the Settings screen where you
can change music and sound volume. You can push the Settings scene to display it, and
its Back button then simply calls popScene ; the game will return to the previous
state. Whether you opened the Settings menu from the main menum, from within the
game, or from someplace else, this technique works fine and lets you avoid having to
keep track of where the Settings menu was opened.
Another case where pushScene and popScene are very useful is if you want to re-
tain the state of the initial scene without having to revert to saving and loading that
scene's state. For example, you could push the scene that shows the current leaderboard
in a multiplayer game and then pop it again without having to save and load the game
state, because the game scene remains in memory. And the game hasn't advanced
while the player was inspecting the leaderboard because the game scene was automat-
ically paused while the leaderboard scene was visible.
However, you need to ensure that there's always enough additional free memory avail-
able for the pushed scene at any time the scene could be pushed, which is hard to test
for. Any scene you may want to push should therefore be very lightweight, consume
little memory, and should only pop itself but never push other scenes or even call re-
placeScene .
For example, to display a Settings scene from anywhere on top of the current scene,
use this code:
[[CCDirector sharedDirector] pushScene:[Settings scene]];
Now inside the Settings scene you need to call popScene when you want to go back
to the previous scene that's still in memory:
[[CCDirector sharedDirector] popScene];
Search Nedrilad ::

Custom Search