Book Image

Cocos2d-x by Example: Beginner's Guide

By : Roger Engelbert
Book Image

Cocos2d-x by Example: Beginner's Guide

By: Roger Engelbert

Overview of this book

Table of Contents (19 chapters)
Cocos2d-x by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 current scene, if any (all their destructors will be called), and a new layer will be instantiated 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 create a new instance of MenuLayer and add it as a child to the new scene...