I’m new to using CCAnimations in cocos2D, and I’ve run into a problem that I’ve had a hard time solving.
I’m making a basic platforming game, and the player sprite has various animations that need to be run depending on the state of the player.
I have the following code in my layer’s init method:
sprite = [CCSprite spriteWithSpriteFrameName:@"stand.png"];
standingSprites = [NSArray arrayWithObjects:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"stand.png"],
nil];
runningSprites = [NSArray arrayWithObjects:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"run1.png"],
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"run2.png"],
nil];
standingAnimation = [CCAnimation animationWithFrames:standingSprites delay:0.2f];
runningAnimation = [CCAnimation animationWithFrames:runningSprites delay:0.2f];
animationAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:standingAnimation]];
[sprite runAction:animationAction];
This works as expected with either of the two animations. However, I want to run standingAnimation when the player is standing still and runningAnimation when the player is running. I attempted to do this as follows:
-(void)walk {
if(!isWalking) {
isWalking = true;
[sprite stopAction:animationAction];
animationAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:runningAnimation]];
[sprite runAction:animationAction];
}
}
The second to the last line crashes the program, causing an EXC_BAD_ACCESS (on referencing 0x0). Stepping through walk in the debugger, it doesn’t appear that any of the relevant pointers are null.
From the stack trace:
2012-06-03 10:59:59.907 ThirtyMinutes[9876:6403] *** Terminating app
due to uncaught exception 'NSInvalidArgumentException', reason:
'-[NSCTFontDescriptor frames]: unrecognized selector sent to instance
0x7f808d93e9f0'
0x7f808d93e9f0 is the address of runningAnimation.
- What am I doing wrong?
- Is there a better way of doing this?
Thanks!
The error message is telling you that, at the moment of the error, 0x7f808d93e9f0 is the address of a NSCTFontDescriptor object. The likely reason is that you’re not retaining
runningAnimationand its memory has been reclaimed for a different object.(Show the declaration of
runningAnimationif its not obvious how that’s happening.)