Search code examples
iphoneanimationcocos2d-iphonesprite

Animation in Cocos2d.


I am trying to make a sprite animate in cocos2D. I believe I have the animation set up, but how do I draw the animating sprite onto the screen? Here is what I have:

id anim = [[[CCAnimation alloc] initWithName:@"char_walking" delay:1/12.0] autorelease];

[anim addFrame:@"run2.png"];
[anim addFrame:@"run1.png"];
[anim addFrame:@"run3.png"];
[anim addFrame:@"run4.png"];
[anim addFrame:@"run3.png"];
[anim addFrame:@"run1.png"];

id myAction = [CCAnimate actionWithAnimation:anim];
id repeating = [CCRepeatForever actionWithAction:myAction];

[character do:repeating];

character = [CCSprite spriteWithSpriteFrame:anim];
character.position = ccp(160, 240);
[self addChild:character];

Thanks in advance, John


Solution

  • Maybe it was just a cut-and-paste error, but it looks like you're telling the sprite to repeat the animation BEFORE you create it, so the character sprite you are adding to the node never gets the CCAnimate action sent to it.