1、创建方法
//创建动画动作 -(CCAction *)createAnimation:(NSString *)fishFileName delay:(float)delay typeFish:(int)typeFish PicNum:(int)PicNum{ NSLog(@" delay: %f,typeFish:%d,PicNum:%d",delay,typeFish,PicNum); NSMutableArray *frames=[NSMutableArray array]; for(int i=1;i<=PicNum;i++) { NSString *fileName=[NSString stringWithFormat:@"fish%d_%d_0.png",typeFish,i]; NSLog(@" %@",fileName); CCSprite *sFish=[CCSprite spriteWithFile:fileName]; if(sFish==nil){ break; } CCSpriteFrame *frame=[CCSpriteFrame frameWithTexture:sFish.texture rect:sFish.textureRect]; [frames addObject:frame]; } CCAnimation *anima=[CCAnimation animationWithFrames:frames delay:delay]; id act=nil; act=[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:anima restoreOriginalFrame:NO]]; return act; }
2、调用
id ac=[self createAnimation:nil delay:0.3 typeFish:ranTypeFish PicNum:6]; CCSprite *sprite=[CCSprite spriteWithFile:[NSString stringWithFormat:@"fish%d_1_0.png",ranTypeFish]]; [self addChild:sprite]; [sprite runAction:ac];