Game Development Reference
In-Depth Information
CCLayer* layer = [HelloWorld node];
[scene addChild:layer];
CCLayer* userInterfaceLayer = [HelloWorldUserInterface node];
[scene addChild: userInterfaceLayer];
return scene;
This scene now has three distinct layers: the backgroundLayer , the regular game
object layer , and on top of that the userInterfaceLayer . Because the layers
are added to the scene in the order created, any nodes added to the back-
groundLayer will be drawn behind the other layers. Likewise, nodes added to the
userInterfaceLayer will always be drawn on top of any nodes in the layer
and backgroundLayer .
Tip Again, strictly speaking, a layer does not have to be derived from the
CCLayer class; it could also be a simple CCNode . This is often preferable
when you need the layer only to group nodes together and don't require it to
handle user input.
One case where you might want to use multiple layers per scene is if you have a
scrolling background and a static frame surrounding the background, possibly includ-
ing user interface elements. Using two separate layers makes it easy to move the back-
ground layer by simply adjusting the layer's position while the foreground layer re-
mains in place. In addition, all objects of the same layer will be either in front of or be-
hind objects of another layer, depending on the z-order of the layers. Of course, you
can achieve the same effect without layers, but it requires each individual object in the
background to be moved separately. That's ineffective, so avoid it if you can.
Like scenes, layers have the same dimension as the cocos2d OpenGL view. For iOS
devices, this is almost always the size of the screen; on Mac OS X, it's the size of the
Layers are primarily a grouping concept. For example, you can use any action with a
layer, and the action will affect all the objects on the layer. That means you can move
all layer objects around in unison or rotate and scale them all at once. In general, use a
Search Nedrilad ::

Custom Search