Game Development Reference
In-Depth Information
Time for action - creating a scene
transition
You have, of course, been using scenes all along.
1. Hidden in
AppDelegate.cpp
, you've had lines like:
auto scene = GameLayer::scene();
// run
director->runWithScene(scene);
2. 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 cur-
rent scene, if any (all their destructors will be called), and a new layer will be in-
stantiated and wrapped inside the new
Scene
.
3. 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;
}
4. The static
MenuLayer::scene
method will create a blank scene, and then cre-
ate a new instance of
MenuLayer
and add it as a child to the new scene.
5. Now you can tell
Director
to run it as follows:
Director::getInstance()->replaceScene(MenuLayer::scene());
6. The logic changes a little if you wish to use a transition effect. So, inside our
MenuLayer.cpp
class, this is how we transition to
LevelSelectLayer
: