Animation in Cocos2d-iphone

Animation in Cocos2d-iphone v2.0.0

What you need

1.a png file -- default.png
2.a plist file -- default.plist (anim01.png -- anim10.png)
    (you can get png file and plist file from: )
         Zwoptex http://zwoptexapp.com          TexturePacker http://texturepacker.com

Cocos2d-iphone code

1.load plist file to CCSpriteFrameCache

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: @"default.plist"];

2.load png file to CCSpriteBatchNode

CCSpriteBatchNode *spriteBatchNode = [CCSpriteBatchNode batchNodeWithFile: @"default.png"];

3.add batch node to layer

[self addChild: spriteBatchNode];

4.create a sprite from frame cache

animSprite = [CCSprite spriteWithSpriteFrameName: @"anim01.png"];

5.add sprite to layer

[self addChild: animSprite];

6.create animation

CCAnimation* animation = [CCAnimation animation];

7.add frame from frame cache to animation

[animation addSpriteFrame: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"anim02.png"]];
[animation addSpriteFrame: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"anim03.png"]];
[animation addSpriteFrame: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"anim04.png"]];
[animation addSpriteFrame: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"anim05.png"]];
[animation addSpriteFrame: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"anim06.png"]];
[animation addSpriteFrame: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"anim07.png"]];
[animation addSpriteFrame: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"anim08.png"]];
[animation addSpriteFrame: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"anim09.png"]];
[animation addSpriteFrame: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"anim10.png"]];

8.set some properties of animation

[animation setDelayPerUnit: 0.02f];
[animation setRestoreOriginalFrame: YES];
[animation setLoops: 100];

9.create animation action

id animationaction = [CCAnimate actionWithAnimation: animation];

10.run animation action

[animSprite runAction: animationaction];

 

posted on 2013-01-02 15:52  紫澜  阅读(267)  评论(0编辑  收藏  举报