不使用纹理图册创贱动画
NSMutableArray *frames=[NSMutableArray arrayWithCApacity:4];
for(int i=0;i<4;i++){
NSString *file=[NSString stringWithFormat:@"images%i.png",i];
CCTexture2D *texture=[[CCTextureCache]addImage:file];
CGSize texSize=[texture contentSize];
CGRect texRect=CGRectMake(0,0,texSize.width,texSize.height);
CCSpriteFrame *frame=[CCSpriteFrame frameWithTexture:texture rect:texRect];
[frames addObject:frame];
}
CCAnimation *anim=[CCAniomation animationWithFrames:frames delay:0.8f];
CCAnimate *animate=[CCAnimate actionWithAnimation:anim];
CCRepeatForever *repeat=[CCRepeatForever actionWithAction:animate];
[self runAction:repeat];
CCAnimationCache类用来存储可按名称访问的动画
CCAnimation *anim=[CCAnimation animationWithFrames:frames delay:1];
[[CCAnimationCache sharedAniametionCache]addAnimation:anim name:@"move"];
CCAnimation *move=[[CCAnimationCache sharedAnimationCache]animationByName:@"move"];
用于创建动画的辅助类别
由于创建动画帧和动画代码对所有的动画通用,因此应该考虑将代码封装到一个辅助方法中,使用OC中的类别扩展CCAnimation类。
@interface CCAnimation(Helper) +(CCAnmationimation *)animationWithFile:(NSString *)name frameCount:(int)franeCount delay:(float)delay; @end
接收三个参数---文件名,帧数和动画延时
实现
{
NSMutableArray *frames=[NSMutableArray arrayWithCapacity:frameCount];
for(int i=0;i<frameCount;i++){
NSString *file=[NSString stringWIthFormat:@"%@%i.png",name,i];
CCTexture2D *texture=[[CCTextureCache shareTextureCache]addImage:file];
CGSize texSize=texture.contentSize;
CGRext texRect=CGRextMake(0,0,texSize.width,texSize.height);
CCSpriteFrame *frame=[CCSpriteFrame frameWithTexture:texture rect:texRect];
}
return [CCAnimation animationWithFrames:frames delay:delay];
}