Question

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.

Was it helpful?

Solution 3

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

OTHER TIPS

try with this code..

[_guy runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation: walkingAnimation restoreOriginalFrame:NO]]];

First of all, there is no need for this:

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

Instead, you can jut write this:

[walkingFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"Frame-%03i.tif", i]]];

Having said this, can you be more specific about the error? What exactly is the error msg and at which line you're getting it? If you're getting it on this line:

CCAnimation *walkingAnimation = [CCAnimation animationWithSpriteFrames:walkingFrames delay:animationWalkingSpeed];

I suggest you should first print out the frames in your CCSpriteFrameCache and see if those frames appear there or not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top