Developers Manual > Cocos2d-x > Modules > Scene Graph > Layer

Layer

Layer seems to useless since v3.0, because now all Nodes can receive touch events. It is reserved just for compatibility. It may be removed in future.

Layers may contain any Node as a child, including Sprites, Labels, and even other Layer objects. Because layers are a subclass of Node, they can be transformed manually or by using Actions. See Actions for more information.

Multiple Layers Example:

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
auto layer1 = LayerGradient::create(Color4B(255, 0, 0, 255), Color4B(255, 0, 255, 255));
layer1->setContentSize(Size(80, 80));
layer1->setPosition(Vec2(50, 50));
addChild(layer1);

auto layer2 = LayerGradient::create(Color4B(0, 0, 0, 127), Color4B(255, 255, 255, 127));
layer2->setContentSize(Size(80, 80));
layer2->setPosition(Vec2(100,90));
addChild(layer2);

auto layer3 = LayerGradient::create();
layer3->setContentSize(Size(80, 80));
layer3->setPosition(Vec2(150,140));
layer3->setStartColor(Color3B(255, 0, 0));
layer3->setEndColor(Color3B(255, 0, 255));
layer3->setStartOpacity(255);
layer3->setEndOpacity(255);
BlendFunc blend;
blend.src = GL_SRC_ALPHA;
blend.dst = GL_ONE_MINUS_SRC_ALPHA;
layer3->setBlendFunc(blend);
addChild(layer3);

lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
local layer1 = cc.LayerGradient:create(cc.c4b(255, 0, 0, 255), cc.c4b(255, 0, 255, 255));
layer1:setContentSize(cc.size(80, 80));
layer1:setPosition(cc.p(50, 50));
addChild(layer1);

local layer2 = cc.LayerGradient:create(cc.c4b(0, 0, 0, 127), cc.c4b(255, 255, 255, 127));
layer2:setContentSize(cc.size(80, 80));
layer2:setPosition(cc.p(100,90));
addChild(layer2);

local layer3 = cc.LayerGradient:create();
layer3:setContentSize(cc.size(80, 80));
layer3:setPosition(cc.p(150,140));
layer3:setStartColor(cc.c3b(255, 0, 0));
layer3:setEndColor(cc.c3b(255, 0, 255));
layer3:setStartOpacity(255);
layer3:setEndOpacity(255);
layer3:setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
addChild(layer3);

multi-layers

multi-layers.png (59.9 kB) owen, 2014-04-01 08:07

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.