Search code examples
animationxcode4cocos2d-iphoneccspritesprite-sheet

Animating a Sprite with a Sprite Sheet gives an uknown error


I am trying to get a sprite to animate itself forever. There are no problems, and it builds fine. I get past the menus and when I click on the scene that has my sprite that I want to animate on it, it crashes. I am using the following code for my animation:

        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"sprite_fly.plist"];
    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"sprite_fly.png"];
    [self addChild:spriteSheet];
    NSMutableArray *flapAnimFrames = [NSMutableArray array];
    for(int i = 1; i<=6; ++i) {
        [flapAnimFrames addObject:
            [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
             [NSString stringWithFormat:@"Fly%d.png"]]];
    }
    CCAnimation *flapAnim = [CCAnimation animationWithFrames:flapAnimFrames delay:1];
    CGSize winSize = [CCDirector sharedDirector].winSize;
    fly = [CCSprite spriteWithSpriteFrameName:@"fly1.png"];
    fly.position = ccp(winSize.width/2, winSize.height/2);
    flapAction = [CCRepeatForever actionWithAction:
                  [CCAnimate actionWithAnimation:flapAnim restoreOriginalFrame:NO]];
    [fly runAction:flapAction];
    [spriteSheet addChild:fly];

I think that the problem is to do with the first line of code, CCSpriteFrameCache, but I can't see anything wrong with it. Please help, or give me another way to animate my sprite.


Solution

  • I think this is the best example for animation in cocos2d.click here.

    I am not sure but i think the problem is with your sprite images or image name. check your image name in the plist file. Make sure that all images have same size which you are using for your animation.

    Hope that will help you.