A Scene
is a container that holds Sprites
, Layers
and other rendered content. A Scene
is responsible for running game logic and rendering the content on a per-frame basis.
It is very easy to create a Scene
// create a simple scene
cocos2d::Scene* myScene = cocos2d::Scene::create();
// create a simple scene with physics
cocos2d::Scene* scene = cocos2d::Scene::createWithPhysics();
A Node
tree controls the draw order of GUI elements. This might also be referred to as scene graph priority
. The important thing to remember is that this defines the drawing order of the GUI elements, starting first to last.
A simple Node
tree laid out in code:
// create a simple scene
auto myScene = cocos2d::Scene::create();
// create a simple layer
auto myLayer = cocos2d::Layer::create();
// a few Sprites
auto sprite1 = cocos2d::Sprite::create("sprite.png");
auto sprite2 = cocos2d::Sprite::create("sprite.png");
auto sprite3 = cocos2d::Sprite::create("sprite.png");
// a few more Sprites
auto spriteA = cocos2d::Sprite::create("sprite2.png");
auto spriteB = cocos2d::Sprite::create("sprite2.png");
auto spriteC = cocos2d::Sprite::create("sprite2.png");
sprite1->addChild(spriteA); // spriteA is now a child of sprite1
sprite2->addChild(spriteB); // spriteB is now a child of sprite2
sprite3->addChild(spriteC); // spriteC is not a child of sprite3
myLayer->addChild(sprite1); // sprite1 is now a child of myLayer
myLayer->addChild(sprite2); // sprite2 is now a child of myLayer
myLayer->addChild(sprite3); // sprite3 is now a child of myLayer
myScene->addChild(myLayer); // myLayer is not a child of myScene
An important thing to remember is that properties applied to Node
are cascaded down to its children.
We all know about coordinate systems from various school math classes.
You might need to move between Scenes
in your game. Perhaps starting a new game, changing levels or even ending your game. Cocos2d-x provides a number of ways to do Scene Transitions
.
auto myScene = cocos2d::Scene::create();
// runWithScene - use this for the first Scene only
cocos2d::Director::getInstance()->runWithScene(myScene);
// replaceScene - replace a scene outright
cocos2d::Director::getInstance()->replaceScene(myScene);
// pushScene - suspends the execution of the running scene, pushing it on the stack
// of suspended scenes. Only call this if there is a running scene
cocos2d::Director::getInstance()->pushScene(myScene);
// popScene - This scene will replace the running one. The running scene will be
// deleted. Only call this if there is a running scene
cocos2d::Director::getInstance()->popScene(myScene);
You can add visual effects to your Scene
transitions
auto myScene = cocos2d::Scene::create();
// Transition Fade
cocos2d::Director::getInstance()->replaceScene(cocos2d::TransitionFade::create(0.5, myScene, cocos2d::Color3B(0,255,255)));
// FlipX
cocos2d::Director::getInstance()->replaceScene( cocos2d::TransitionFlipX::create(2, myScene));
// Transition Slide In
cocos2d::Director::getInstance()->replaceScene( cocos2d::TransitionSlideInT::create(1, myScene) );