La hoja de sprites de Cocos2d no está animada

SpriteSheets are one thing I just can't get my head around. I'm looking for a very clear and easy to follow way to learn about them. I have read and studied up on them but still have problems.

I don't know why my code isn't working.

-(void) addPlayer {

CGSize winSize = [[CCDirector sharedDirector] winSize];

CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"walk1.png"];
sprite.position = ccp(winSize.width/2, winSize.height/2);

CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"player-walk.png"];
[batchNode addChild:sprite];
[self addChild:batchNode z:350];






NSMutableArray *frames = [NSMutableArray array];
for(int i = 1; i <= 4; ++i) {
    [frames addObject:
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"walk%d.png", i]]];
}

CCAnimation *anim = [CCAnimation animation];

CCAnimate *animate = [CCAnimate actionWithAnimation:anim];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:animate];
[self runAction:repeat];
}

Why is my player not animating?

preguntado el 29 de agosto de 12 a las 13:08

don't see in your code, where do you use array of frames. you just create empty animation object -

Hi, thanks for your help. I'm trying to get the frames from the plist using CCSpriteFrameCache. -

1 Respuestas

Spritesheet is just a convenient way to store textures.

Your CCAnimation object has no frames. You gather an array of frames, but you never use it. Change your

CCAnimation *anim = [CCAnimation animation];

línea a

CCAnimation *anim = [CCAnimation animationWithFrames:frames delay:0.2f];

If you want to animate a sprite, you have to run animating action on this sprite. Use [sprite runAction:repeat] en lugar de [self runAction:repeat];.

Respondido 29 ago 12, 14:08

Hey, thank you so much for your help with that. I spent all day today trying to figure it out. animationWithFrames is deprecated. I used animationWithSpriteFrames and it worked fine. Thank you again. - El aprendiz

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.