Game Development Reference
In-Depth Information
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();
return _instance;
}
3. An _instance static property is declared at the top of GameLayer.cpp as fol-
lows:
Search WWH ::




Custom Search