Opacity changes not affecting children

Forums Programming cocos2d support (graphics engine) Opacity changes not affecting children

This topic contains 6 replies, has 6 voices, and was last updated by  mobilebros 1 year, 1 month ago.

Viewing 7 posts - 1 through 7 (of 7 total)
Author Posts
Author Posts
August 11, 2009 at 11:45 am #216665


Hi !

I am quite new to cocos2d so maybe I haven’t correctly understood something, but here is my problem.

I have a parent sprite A, with a child sprite B.

If I change opacity on sprite A, for example if I set it to 0, my sprite A become transparent. But my child sprite B is still visible with an opacity of 1.

Is it the normal behaviour ? Is there a way to propagate opacity changes to all children ?

Thanks !!!

August 11, 2009 at 1:35 pm #256278


“Is it the normal behaviour ?”

Unfortunately I think it is. You could make a Sprite subclass and override:

- (void) setOpacity:(GLubyte)opacity
[super setOpacity:opacity];
for( id child in children ) {
[child setOpacity:opacity];

Im not sure if there is a better way, there might be, but this is what I do. HTH, Codemattic

May 22, 2010 at 7:29 pm #256279


Now that honor_transform is in place, the code above seems unnecessary. However, I’m still unable to get opacity to propagate to children CCSprites (scale is working fine).

Is there a trick to making this happen?

I’ve looked through forum posts and code examples to no avail.



May 23, 2010 at 2:10 am #256280


Why you think opacity should be propagated to children? On the other hand, visibility is propagated, so if you want to hide node and its children, then node.visibility = 0 will hide everything…

May 23, 2010 at 2:19 am #256281


@crmagicxxx: If we can selectively honor scale and rotation, why not opacity? In my case, I want an object composed of two different CCSprites to fade away under certain circumstances. I’ve resorted to applying separate (but identical) FadeOut Actions to each CCSprite…it would be more elegant if I could merely apply one Action to the parent object (which is itself derived from CCSprite).



March 16, 2013 at 9:46 am #256282


I already faced this issue of fading a whole hierarchy of nodes.

I frist tried the suggested method of propagating setOpacity to the children but it did not render as I expected because of additive opacities (opacity is applied on each node, not globally, so the result looks darker where nodes overlap).

I you really want to control opacity “globally” on your container you can render it with a CCRenderTexture and change its opacity.

See the following article for more details on how to do that: http://2sa-studio.blogspot.com/2013/01/fading-node-hierarchy-with.html

March 16, 2013 at 3:03 pm #256283


If you’re using the latest code (2.1.0) then there’s a property (BOOL) called cascadeOpacity that’ll work for your needs. http://www.cocos2d-iphone.org/api-ref/2.1.0/protocol_c_c_r_g_b_a_protocol-p.html#a6e2d9225d56cbb998e0ada25d415fd14

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.