Xzhsh
Xzhsh

Reputation: 2239

Changing opacity for multiple sprites in Cocos2d

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!

Upvotes: 2

Views: 2149

Answers (2)

Carter
Carter

Reputation: 3093

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];
    }
}

Upvotes: 3

ScottPetit
ScottPetit

Reputation: 814

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];
}

Upvotes: 0

Related Questions