Game Development Reference
In-Depth Information
Containers
Also important in Cocos2d-x is the notion of containers (or nodes). These are all the ob-
jects that can have sprites inside them (or other nodes.) This is extremely useful at times
because by changing aspects of the container, you automatically change aspects of its chil-
dren. Move the container and all its children will move with it. Rotate the container and
well, you get the picture!
The containers are: Scene , Layer , and Sprite . They all inherit from a base container
class called node . Each container will have its peculiarities, but basically you will arrange
them as follows:
Scene : This will contain one or more Node , usually Layer types. It is common
to break applications into multiple scenes; for instance, one for the main menu, one
for settings, and one for the actual game. Technically, each scene will behave as a
separate entity in your application, almost as subapplications themselves, and you
can run a series of transition effects when changing between scenes.
Layer : This will most likely contain Sprite . There are a number of specialized
Layer objects aimed at saving you, the developer, some time in creating things
such as menus for instance ( Menu ), or a colored background ( LayerColor ). You
can have more than one Layer per scene, but good planning makes this usually
unnecessary.
Sprite : This will contain your images and be added as children to Layer de-
rived containers. To my mind, this is the most important class in all of Cocos2d-x,
so much so, that after your application initializes, when both a Scene and a Lay-
er object are created, you could build your entire game only with sprites and never
use another container class in Cocos2d-x.
Node : This super class to all containers blurs the line between itself and Layer ,
and even Sprite at times. It has its own set of specialized subclasses (besides the
ones mentioned earlier), such as MotionStreak , ParallaxNode , and
SpriteBatchNode , to name a few. It can, with a few adjustments, behave just
as Layer . But most of the time you will use it to create your own specialized
nodes or as a general reference in polymorphism.
Search WWH ::




Custom Search