Scene Managing
In Cocos Creator we use scene filename (without extension) to index scenes, and use the following API to load and switch scene:
cc.director.loadScene("MyScene");
In addition, as of v2.4, the Asset Bundle has added a new loading method:
bundle.loadScene('MyScene', function (err, scene) {
cc.director.runScene(scene);
});
The loadScene
provided by the Asset Bundle will only load scenes from the specified bundle and will not automatically run the scenes, you also need to use cc.director.runScene
to run the scenes.loadScene
also provides more parameters to control the loading process, so developers can control the loading parameters themselves or do some processing after the loading scene.
For more information about loading scenes in the Asset Bundle, you can refer to the Asset Bundle documentation.
Use persist node to managing multiple scenes and pass information among scenes
There's only one scene running at the same time, when switching scenes the previous scene will be destroyed. To easily store and pass information among scenes we can mark a node as persist node to prevent it from being destroyed during scene switch. This way the node and all the component instances attached to it will remain in memory.
cc.game.addPersistRootNode(myNode);
The above API will make myNode
a persist node across scenes. We can store information for player profile or necessary data needed to initialize next scene.
To revert a persist node to normal node:
cc.game.removePersistRootNode(myNode);
Beware the above API will not destroy the node immediately, only mark it as 'destroyable' for next scene switch.
Global variable
We can also store and pass information using global variable. For details please read Global Variable.
Scene loaded callback
When loading scene, you can pass a function as the callback when scene loaded:
cc.director.loadScene("MyScene", onSceneLaunched);
onSceneLaunched
is the callback function declared in the same script.
Since it can only be declared in the same script, it's better to call loadScene
from a persist node so that the component instance will stay in memory for callback function to continue running.
Preload scene
cc.director.loadScene
will destroy previous scene and run new scene right after the new scene is completely loaded. Sometimes we need to load new scene in the background when still running previous scene, and only switch scene when new scene is loaded. We can use cc.director.preloadScene
to do this:
cc.director.preloadScene("table", function () {
cc.log("Next scene preloaded");
});
Once callback is fired, you can call loadScene
at any time to immediately switch scenes.
cc.director.loadScene("table");
It will be totally fine to call cc.director.loadScene
at any time even if the preloading is not yet finished. You can take Black Jack demo as an example.
Note: using
cc.loader.loadRes
to load assets in next scene and callrunScene
method to switch scene is deprecated!
// DO NOT USE THE FOLLOWING METHOD!
cc.loader.loadRes('MyScene.fire', function(err, res) {
cc.director.runScene(res.scene);
});