You have, of course, been using scenes all along.
Hidden in
AppDelegate.cpp
, you've had lines like:auto scene = GameLayer::scene(); // run director->runWithScene(scene);
So, in order to change scenes, all you need to do is tell the
Director
class which scene you wish it to run. Cocos2d-x will then get rid of all the content in the current scene, if any (all their destructors will be called), and a new layer will be instantiated and wrapped inside the newScene
.Breaking the steps down a little further, this is how you usually create a new scene for
Director
:Scene* MenuLayer::scene() { // 'scene' is an autorelease object auto scene = Scene::create(); // add layer as a child to scene auto layer = new MenuLayer(); scene->addChild(layer); layer->release(); return scene; }
The static
MenuLayer::scene
method will create a blank scene, and then create a new instance ofMenuLayer
and add it as a child to the new scene...