Scene Managing: How to set main scene and switch between scenes

It would be great if there was more information on how to set the main scene and how to switch between scenes.

Previously it was known that app.js would always be the first scene/file to run because index would invoke main.js to run, set the resolution policy and all that and then the game would start in app.js.

With Creator how do you set the main scene to always be your first scene and how do you switch between scenes? I made a play button but doing anything like cc.director.runScene(new gameScene()); I get "Uncaught ReferenceError: gameScene is not defined.

To setup the main scene, you can open the build panel, you will see a build option: start scene, which is the main entry scene.

To switch between scene, please try:

cc.director.loadScene('YouSceneAssetName');

In Creator, all scenes is created in the editor, so we need to deserialize it at runtime, that’s why we provided loadScene API, runScene can still work with a deserialized Scene asset, but you need to load it with cc.loader firstly.

@pandamicro
Awesome!
Thanks that worked :slight_smile:

So if I use cc.director.loadScene('SceneAssetName'); then will that stop the previous scene? If I use this to go back and forth from the menu scene to the game scene (play button and exit button) will that be ok or will that cause any memory leak issues?

Great job with Cocos Creator by the way. I love it!

loadScene will stop the previous scene, you can’t go back like the old popScene API, you need to reload the previous scene if you want to go back. As for memory leak, we don’t save the scene asset, it will be deleted once returned from the loader

1 Like

Hi!

I’m trying to go back to a previous scene (I just have 2 scenes) and I can’t find the way to do that.

I tried to use loadScene, resume, and some other methods but they doesn’t pop to the previous scene.

I also need to send some variables generated in the new scene to the previous one is it possible?

Thanks!