Question

I am trying to animate my player for walking directions (like in a birds-eye-view RPG) so I have it if (joystick.velocity. y > 0){ [player runAction: walkUpAction] } of course this causes the problem of Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'runAction: Action already running I get that. What I don't get is a way to work around this. I've tried adding some variables (isRunning, if is running then don't call type thing) but nothing seems to work. I've asked this on the cocos2d forum but no luck. Any ideas?

Was it helpful?

Solution

Try this:

first declare some iVars in the .h

BOOL _isWalkingUp;
BOOL _isWalkingDown;
BOOL _isWalkingLeft;
BOOL _isWalkingRight;

then, in each section of code where you detect a direction change:

if(!_isWalkingUp && ( joystick.velocity.y >0 ) ) {
    [player stopAllActions];
    [player runAction:walkUpAction];
    _isWalkingUp=YES;
    _isWalkingDown=NO;
    _isWalkingLeft=NO;
    _isWalkingRight=NO;
}

etc .... you will probably want to add some 'state' to avoid a jerky motion control. But this would be some kind of a start.

OTHER TIPS

In cocos2d you must recreate action before run, i.e. you cannot run the same action several times. So, it is no need to save your action in variable if you don't want to stop only this action, not the others. This is about your error. If you tell a bit more details, what are you trying to do with your walkUpAction, I can try to give you few suggestions about how to do what you want.

Actually, i don't know how do this joystick works, but if it does not store previous state, you can store it manually and run your action in case of previous velocity of 0 and current velocity more than 0. And, anyway you must recreate action before run it again. So, you will have something like this

- (void) update:(ccTime)dt
{
    BOOL needRunWalkUpAnimation = (prevVelocity == 0) && (curVelocity > 0);
    if( needRunWalkUpAnimation )
    {
        id walkUpAction = \\create your action here
        [player runAction: walkUpAction];
    }

    prevVelocity = curVelocity;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top