0
votes

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?

1
don't see in your code, where do you use array of frames. you just create empty animation object - Morion
Hi, thanks for your help. I'm trying to get the frames from the plist using CCSpriteFrameCache. - The Learner

1 Answers

1
votes

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];

line to

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] instead of [self runAction:repeat];.