Naveed Abbas
Naveed Abbas

Reputation: 1187

cocos2D CCLayer or CCLayerColor setOpacity

I'm trying to animate the opacity of a CCLayerColor and its not working. Here are my efforts so far. I've defined retryMenuLayer in .h like

CCLayerColor *retryMenuLayer;

and in .m file

retryMenuLayer = [CCLayerColor node];
[self addChild:retryMenuLayer z:5];

retryMenuLayer.scale = 0.5;
[retryMenuLayer setOpacity:0];

and in appearing method, I'm calling this.

[retryMenuLayer runAction:[CCFadeIn actionWithDuration:1]];

//OR

[retryMenuLayer setOpacity:255];

What happens is the background of retryMenuLayer animates from transparent to solid black but the contents inside (its children - a Menu with buttons) doesn't animate. In fact I have to use visibility property to at least disappear until the method is called.

retryMenuLayer.visible = NO; // When initiating.
retryMenuLayer.visible = YES; // When need to appear the layer.

Upvotes: 2

Views: 5810

Answers (2)

JOG
JOG

Reputation: 5640

For transparency in CCLayerColor, I use

    CCLayerColor *_shadowLayer = [CCLayerColor layerWithColor: ccc4(0,0,0, 100)];

for a transparent black color.

To make it FadeIn, this should work:

CCLayerColor _shadowLayer = [CCLayerColor layerWithColor: ccc4(0,0,0, 0)];
[_shadowLayer setContentSize: CGSizeMake(_winSize.width, _winSize.height)];
_shadowLayer.anchorPoint = ccp(0.0f, 0.0f); 
_shadowLayer.position = ccp(0, 0);

[self addChild: _shadowLayer];

[_shadowLayer runAction: [CCFadeTo actionWithDuration:1.5f opacity:100]];

Note here that I added the shadow layer to self, that is my own customized layer. If I would add it to a sprite that is faded in, the opacity does not pull through to the shadow layer. There seems to be a workaround on that by user "aerostat" in the link in qklxtlx's answer, though.

Upvotes: 4

Kanglai
Kanglai

Reputation: 530

CCLayer doesn't have opacity. Please refer to this http://www.cocos2d-iphone.org/forum/topic/5088

Upvotes: 3

Related Questions