How do you manage the scenes? Navigating etc?

How do you manage the scenes? Navigating etc?
0

I want to create multiple menu scenes. Example, Im in main menu. i want to navigate to option menu. then i want to go back to main menu. whats the best practice here?

Maybe I create main menu scene from appdelegate. then i can create option menu and call
Director::getInstance()->replaceScene(OptionsMenuScene) from MainMenuScene class. if i want to go back, i can Director::getInstance()->replaceScene(MainMenuScene). provided that optionsmenuscene and mainmenuscene is global and already created/initialized. if for example, you would have multiple levels and each levels is a scene, it would not be nice if i have to create and initialize every levels when starting the application. it would be nice if the level scene is only created when you want to use it.

also, what happens to the scene objects that is being replaced? is it gone?

I believe the usual way people handle transitioning between scenes is by having a loading scene that you transition to between your main menu and level. You don’t load your level until it is needed and when you need it you create it in the loading scene. The app I worked on used this because it wasn’t possible to fit both the main menu and the level in the device’s memory at the same time.

For your options menu, if keeping your main menu in memory isn’t an issue then you probably have 2 options.

The first option is to have your options menu as a layer that sits hidden in your main menu. When you want to display the options menu, hide the main menu layer and show the options menu layer. One downside to this is that any behaviour that happens in the main menu will continue to run while the main menu isn’t visible, so you will have to remember to deal with that. For example, if you scheduled a function call to play a sound every few seconds, it will continue to do so while the main menu is still within the running scene.

The other option is using pushScene() and popScene() instead of replaceScene(). Replacing the main menu scene with the options scene will release it from memory and you will have to recreate the menu scene - and maybe even reload its assets - when you transition back. But pushScene() keeps the current scene in memory while the new scene is used and when you use popScene() then the new scene is removed and the old scene is used again.

Or, if you retain your main menu, then it will stay in memory when using replaceScene(), but you will need to remember to release it when you no longer need it, or else it will take up memory.

I have also seen people mention using 1 scene and just creating their menus and levels as layers, removing “scene” layers when transitioning away and creating and adding level “scene” layers when transitioning to them, but I have personally never used this method.

And in response to your last question: When a scene is replaced, the scene object is released, so if you have not retained it then it will be removed from memory.

2 Likes

@grimfate says it all…

Thanks. Lots of useful information.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.