SimplyKiwi
SimplyKiwi

Reputation: 12444

Fade through image in CCSprite?

Is it possible to fade through the images in a CCSprite? Currently I only know its possible to fade through objects in Cocos2D but I wasn't sure if this was possible or not. Is there any way to do this?

Thanks!

Upvotes: 0

Views: 734

Answers (2)

Binarian
Binarian

Reputation: 12446

You can fade with 2 CCSprites through multiple images.

  1. CCSprite A to opaque to the front zOrder 1
  2. CCSprite B to transparent (if CCSprite A covers the CCSprite B then this is not needed), zOrder 0
  3. FadeOut CCSprite A and after that put it in the background, zOrder 0
  4. CCSprite B zOrder 1 in the front, it is seen after point 2
  5. Exchange image of CCSprite A to every uneven
  6. repeat everything, this time give the argument of CCSprite A and B in opposite order

Exchange the sprite:

CCCallBlock *exchangeImage = [CCCallBlock actionWithBlock:^{
    sprite.texture = [[CCTextureCache sharedTextureCache] addImage:@"image.png"];
}];

Add delay if needed:

CCDelayTime *delayAction = [[CCDelayTime alloc] initWithDuration:0.4f];

Put together an repeat:

CCSequence *exchangeSequence = [CCSequence actions:exchangeImage, delayAction, nil];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:exchangeSequence];
[self runAction:repeat];

Upvotes: 0

Saturn
Saturn

Reputation: 18159

Well, you can use CCFadeOut to remove your current image, while, at the same time, create your new sprite at the same position with opacity 0, and make it fade in with CCFadeIn. If you do it at the same time, you should get a nice effect.

Upvotes: 3

Related Questions