I'm working on an iphone game that uses lots of different animated shrites which have to be drawn in 4 different directions up, down, left and right. I've been playing with the new cocos2d .99.3 and I'm finding the way we animate a little more complicated than before. Here is the current code I have to write for spritesheet animation.
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"animations/grossini.plist"];
CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"grossini_dance_01.png"];
sprite.position = ccp( 100, 100);
CCSpriteSheet *spritesheet = [CCSpriteSheet spriteSheetWithFile:@"animations/grossini.png"];
[spritesheet addChild:sprite];
[self addChild:spritesheet];
NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i < 15; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"grossini_dance_%02d.png",i]];
[animFrames addObject:frame];
}
CCAnimation *animation = [CCAnimation animationWithName:@"dance" frames:animFrames];
// 14 frames * 0.2sec = 2,8 seconds
[sprite runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithDuration:2.8f animation:animation restoreOriginalFrame:NO] ]];
I thought the whole point of using a spritesheet was so you didn't have to have tons of separate sprite files for animation. Is there a way to do this where i just tell it what spritesheet to use and then iterate though the frames?