NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 0; i < 14; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"grossini_dance_%02d.png",(i+1)]];
[animFrames addObject:frame];
}
CCAnimation *animation = [CCAnimation animationWithName:@"dance" delay:0.2f frames:animFrames];
[sprite runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];
This is quite handy. However, i think this is not neat enough.
What i do is create an CCAnimatedSprite which inherit CCSprite. Which will retrieve all frame information from a plist file.
Advantages:
- clearer code
- a more centralized and flexible approach
- can set delay time between frame
- a future support for Animation Editor
Disadvantage:
- need more loading time
- need to create a plist file for each animation
To use CCAnimatedSprite, you need to first create a animation plist file. Please download the source file and take a look at uggyAnimation.plist.
You can use uggyAnimation.plist as a template and add any animation you need. Basically, the uggyAnimation.plist is based on zwoptex spritesheet. You need to indicate the coordinate file and the image source file generated from zwoptex.
After that, all you need is create animation and add frame for the animation.
You may take a look at the Walk_FlipY aniamtion in the uggyAnimation.plist. It contains 4 items. Each item is a frame for Walk_FlipY aniamtion. What you need to provide for this frame are
1. FrameName - which is the frame name refer to the name in zwoptex spritesheet
2. Time - the delay time for this frame
3. FlipX - should the frame flip horizontal
4. FlipY - should the frame flip vertical
After you create the animation plist file, what left is simple use CCAnimatedSprite to play the aniamtion by the animation name using following code.
animatedSprite3 = [[CCAnimatedSprite alloc] initWithAnimationFile:@"uggyAnimation.plist"];
[animatedSprite3 playAnimation:@"Walk_FlipY"];
animatedSprite3.position = ccp(380,260);
animatedSprite3.shouldLoop = true;
//let the sprite handle the animation
[animatedSprite3 schedule:@selector(tick:)];
FTP: Download Source Here



