Skip to content

Latest commit

 

History

History
90 lines (67 loc) · 3.41 KB

6.md

File metadata and controls

90 lines (67 loc) · 3.41 KB

Chapter 6: Building and Transitioning Scenes

What is a Scene?

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.

Creating a Scene

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();

Creating a Node Tree

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

Node properties to its descendants

An important thing to remember is that properties applied to Node are cascaded down to its children.

Coordinate Systems

We all know about coordinate systems from various school math classes.

Converting between coordinate systems

Transitioning between Scenes

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.

Ways to transition between Scenes

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);

Transition Scenes with effects

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) );