Reputation: 1643
I am really really new to cocos2d and o.c programming, and I have been trying to figure out how the sprite animation works. Due to the latest update I get a lot of old code which doesn't work anymore.
I have the following (a spriteheet with 3 sprites which i want to animate)
somehow it keeps crashing
this is in my init method
[[CCSpriteFrameCache sharedSpriteFrameCache ] addSpriteFramesWithFile:@"eno_glasses.plist" ];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"eno_glasses.png"];
[self addChild:spriteSheet];
_body = [CCSprite spriteWithSpriteFrameName: [NSString stringWithFormat:@"eno_glasses02.png"]];
[spriteSheet addChild:_body];
_body.position = CGPointMake(screenSize.width / 2, screenSize.height / 2);
NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i < 3; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"eno_glasses0%d.png",i]];
[animFrames addObject:frame];
}
CCAnimation *animation = [CCAnimation animationWithFrames:animFrames delay:0.03f];
CCAnimate* anime = [CCAnimate actionWithAnimation:animation];
[self runAction:anime];
I know the images can be found because this works (and I can get the images from the plist 01,02,03)
am i forgetting a nil somewhere?
[[CCSpriteFrameCache sharedSpriteFrameCache ] addSpriteFramesWithFile:@"eno_glasses.plist" ];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"eno_glasses.png"];
[self addChild:spriteSheet];
_body = [CCSprite spriteWithSpriteFrameName: [NSString stringWithFormat:@"eno_glasses02.png"]];
[spriteSheet addChild:_body];
_body.position = CGPointMake(screenSize.width / 2, screenSize.height / 2);
Upvotes: 1
Views: 239
Reputation: 9079
use :
[_body runAction:anime];
as for repeating (assuming once)
id anime1=[CCAnimate actionWithAnimation:animation];
id delay=[CCDelayTime actionWithDuration:2.0f];
id anime2=[CCAnimate actionWithAnimation:animation];
id twice = [CCSequence actions:anime1,delay,anime2,nil];
[_body runAction:twice];
also, you may want to chose "eno_glasses01.png" as your start frame.
Upvotes: 2