Fork me on GitHub

创建 和使用 CCAnimation

1- 直接加载图片文件作为frame

CCAnimation *robotAnim = [CCAnimation animation]; // 4  
[robotAnim addFrameWithFilename:@"an1_anim2.png"]; // 5
[robotAnim addFrameWithFilename:@"an1_anim3.png"];
[robotAnim addFrameWithFilename:@"an1_anim4.png"];
id robotAnimationAction = [CCAnimate actionWithDuration:0.5f
animation:robotAnim restoreOriginalFrame:YES]; // 6

2- 通过预加载FrameCache文件作为frame,frameName的图片必须已经加载到FrameCache中。

// Animation example with a CCSpriteBatchNode  
CCAnimation *exampleAnim = [CCAnimation animation];
[exampleAnim addFrame: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"sv_anim_2.png"]];
[exampleAnim addFrame: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"sv_anim_3.png"]];
[exampleAnim addFrame: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"sv_anim_4.png"]];
id animateAction = [CCAnimate actionWithDuration:0.5f animation:exampleAnim restoreOriginalFrame:NO];
id repeatAction = [CCRepeatForever actionWithAction:animateAction];

PS. 可以把Animation加入到AnimationCache

[[CCAnimationCache sharedAnimationCache]   
addAnimation:animationToCache
name:@"AnimationName"];

在需要时取出

CCAnimation *myAnimation = [[CCAnimationCache sharedAnimationCache]  
animationByName:@"AnimationName"];

这样就不用总持有animation。




posted on 2012-04-05 11:57  pengyingh  阅读(193)  评论(0)    收藏  举报

导航