//CCSprite+Animation.h
#import "CCSprite.h" @interface CCSprite (Animation) + (void)fadeWithOldSprite:(CCSprite *)oldSprite newSprite:(CCSprite *)newSprite callFuncTarget:(id)target callFuncAction:(SEL)action fadeDuration:(NSTimeInterval)duration;
@end //CCSprite+Animation.m
#import "CCSprite+Animation.h"
#import "CCActionInterval.h"
#import "CCActionInstant.h" @implementation CCSprite (Animation) + (void)fadeWithOldSprite:(CCSprite *)oldSprite newSprite:(CCSprite *)newSprite callFuncTarget:(id)target callFuncAction:(SEL)action fadeDuration:(NSTimeInterval)duration
{
[oldSprite.parent addChild:newSprite z:oldSprite.zOrder];
CCFadeOut *fadeOut = [CCFadeOut actionWithDuration:duration];
CCCallFunc *callFunc = [CCCallFunc actionWithTarget:target selector:action];
CCSequence *sequence = [CCSequence actionWithArray:@[fadeOut, callFunc]];
[oldSprite runAction:sequence]; CCFadeIn *fadeIn = [CCFadeIn actionWithDuration:duration];
[newSprite runAction:fadeIn];
}
@end
实现3秒切换图片
- (void)startToFadeSprite
{
static NSString *imageNames[] = {
@"[email protected]",
@"[email protected]",
@"[email protected]",
@"[email protected]"
};
static NSInteger index = ;
static NSInteger imageCount = ; if (index == imageCount) {
index = ;
} if (fading == NO) {
fading = YES;
photoTemp = [CCSprite spriteWithFile:imageNames[index]];
photoTemp.textureRect = photo.textureRect;
photoTemp.anchorPoint = photo.anchorPoint;
photoTemp.position = photo.position;
photoTemp.scaleX = photo.scaleX;
photoTemp.scaleY = photo.scaleY;
[CCSprite fadeWithOldSprite:photo newSprite:photoTemp callFuncTarget:self callFuncAction:@selector(fadeFinished) fadeDuration:0.3];
index++;
}
} - (void)fadeFinished
{
[self removeChild:photo];
photo = photoTemp;
fading = NO; CCDelayTime *delay = [CCDelayTime actionWithDuration:2.7];
CCCallFunc *callFunc = [CCCallFunc actionWithTarget:self selector:@selector(startToFadeSprite)];
[self runAction:[CCSequence actions:delay, callFunc, nil]];
} //init
photo = [CCSprite spriteWithFile:@"[email protected]"];
[self addChild:photo]; //just after
[self startToFadeSprite];