Search code examples
cocos2d-iphoneopacityccsprite

Changing the opacity of a CCSprite in a CCSpriteBatchNode


Can the opacity of a CCSprite in a CCSpriteBatchNode be modified?

The following doesn't seem to work:

((CCSprite *)[batchNode getChildByTag:myTag]).opacity = 0.5;

The sprite just disappears instead of showing up with modified opacity.


Solution

  • This is why it's a good idea to look at the types. opacity property of CCSprite is GLubyte. Looking at GLubyte's typedef, we see that it's actually unsigned char, which means that it takes integer values from 0 to 255. With sprite.opacity = 0.5 you implicitly convert floating point value to unsigned char, fractional part of 0.5 is truncated, and sprite.opacity becomes 0.