iphonecocos2d-iphoneccaction

CCAnimation is not working


In my init method I init frames for an animation like this.

    walkingFrames = [NSMutableArray array];
    for(int i =2; i<34; i = i+2){
        if(i<10){
            [walkingFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"Frame-00%d.tif", i]]];
        }
        else{
            [walkingFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"Frame-0%d.tif", i]]];
        }
    }


 where walkingFrames is defined in the header file.

When I try to create an animation like this in my update method I get a bad_access.

    CCAnimation *walkingAnimation = [CCAnimation animationWithSpriteFrames:walkingFrames delay:animationWalkingSpeed];
                    self.walkAction = [CCRepeatForever actionWithAction:
                                       [CCAnimate actionWithAnimation:walkingAnimation]];
                    [_guy runAction:_walkAction];

where walkAction is a CCAction property. 

I am having a hard time solving what is going wrong.


Solution

  • I discovered two problems that were leading to the crash, and then the animation not running. Firstly I should have been initializing my NSMutableArray like this, since I was using it in my update methods:

        walkingFrames = [[NSMutableArray alloc] init];
    

    Instead of how I was doing it before like this:

        walkingFrames = [NSMutableArray array];
    

    Then I found my animation wasn't running, but it wasn't crashing either.

    It was a problem with my CCSpriteFrameCache.

    I was setting it up like this:

        CCSpriteFrameCache *frames = [[CCSpriteFrameCache sharedSpriteFrameCacher] addSpriteFramesWithFile:@"images.plist"];
    

    I should have been doing this:

        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"images.plist"];
    

    And to restore the original frame of my animation when I want to stop it I needed to do this:

        [_guy stopAction:_walkAction];
        [_guy setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"Frame-008.tif"]];