2
votes

I'm relatively new to iphone programming, and I've been starting out with cocos2d. I was wondering if there was a way to set opacity for multiple sprites at once? I noticed that opacity isn't implemented for CCLayer, and opacity doesn't seem to propagate down to children of class CCSprite.

Is there any better way to do this than to override setOpacity on a custom ccnode and iterate through and set opacities individually? Or perhaps overriding draw and setting the blending mode manually?

Thanks!

2

2 Answers

3
votes

You can loop through all the sprites in your scene, check if they are the sprites you want, then set their opacity like this (assuming you set their tag to OPACITY_SPRITE_TAG when you create the sprites)

for(CCSprite* sprite in [self children])
{
    if([sprite tag] == OPACITY_SPRITE_TAG)
    {
        [sprite setOpacity:NEW_OPACITY];
    }
}
0
votes

Couldn't you just increment through an array of sprites that you want to change. Something like:

for(int i = 0; i < [myArray count]; i++){
CCSprite *mySprite = [myArray objectAtIndex:i];
[mySprite setOpacity:100];
}