Book Image

Cocos2d-x Cookbook

By : Akihiro Matsuura
Book Image

Cocos2d-x Cookbook

By: Akihiro Matsuura

Overview of this book

Table of Contents (18 chapters)
Cocos2d-x Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating layers


A layer is an object that can be used on Scene. It is a transparent sheet similar to Photoshop's layer. All the objects are added to Layer in order to be displayed on the screen. Further, a scene can have multiple layers. Layers are also responsible for accepting inputs, drawing, and touching. For example, in the game, a scene has a background layer, hud layer, and a player's layer. In this recipe, we will explain how to use Layer.

How to do it...

The following code shows how to create a layer and add it to a scene:

auto layer = Layer::create();
this->addChild(layer);

That's easy. If you have a color layer, you can do it.

auto layer = LayerColor::create(Color4B::WHITE);
this->addChild(layer);

How it works...

The Scene class is the one displayed on the screen, but the Layer class can be stacked in many layers. Scene has one or more layers, and Sprite has to be on a layer. The Layer class is a transparent sheet. In addition, a transparent node needs more CPU power. So, you need...