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 transitions with a singleton Layer class


We first need to make sure the layer in question can only be instantiated once.

  1. The scene static method in GameLayer looks like this:

    Scene* GameLayer::scene(int level, int levelsCompleted)
    {
        // 'scene' is an autorelease object
        auto scene = Scene::create();
       // add layer as a child to scene
        scene->addChild(GameLayer::create(level, levelsCompleted));
       return scene;
    }

    This layer receives two parameters when created: the game level it should load and the number of levels completed by the player. We create a new Scene object and add GameLayer as its child.

  2. But take a look at the static create method in GameLayer:

    GameLayer * GameLayer::create (int level, int levelsCompleted) {
        if (!_instance) {
            _instance = new GameLayer();
        } else {
            _instance->clearLayer();
        }
        _instance->setLevelsCompleted(levelsCompleted);
        _instance->loadLevel(level);
        _instance->scheduleUpdate...