Question

How do I check if a SKAction has finished its animation?

I need to check if my action has already finished or is still performing its action. After that I want to create a boolean to avoid multiple actions during the main action.

SKAction *lionJumpActionComplete = [lionNode actionForKey:@"lionIsJumping"];
    lionJumpActionComplete = [SKAction sequence: @[lionJumpActionUp, lionJumpActionFly, lionJumpActionDown, lionJumpActionPause]];

    if (lionJumpActionComplete) {
        return;
    }

    [lionNode runAction:lionJumpActionComplete withKey:@"lionIsJumping"];
Was it helpful?

Solution

If this is the only action running on your node, you can check this using:

if (!lionNode.hasActions) { // check if no actions are running on this node
   // action code here
}

Alternatively, you can set your boolean in a completion block that gets called after the action runs and completes:

[lionNode runAction:[SKAction sequence: @[lionJumpActionUp, lionJumpActionFly, lionJumpActionDown, lionJumpActionPause]] completion:^{
    BOOL isActionCompleted = YES;
}];

OTHER TIPS

Here's an example of me creating a walking animation on a node. Before I create it again I make sure the previous one has finished by looking for its key.

    SKAction *animAction = [self actionForKey:@"WalkingZombie"];

    if (animAction) {
        return; // we already have a running animation
    }

    [self runAction:
        [SKAction animateWithTextures:[self walkAnimationFrames]
                         timePerFrame:1.0f/15.0f
                               resize:YES
                              restore:NO]
            withKey:@"WalkingZombie"];
}

You need to check to see if the node is running the action

so in this case

if (![self hasActions]) {
     [self runAction:[self actionForKey:@"ZombieAction"]];
}

probably better might be

[self runAction:[SKAction repeatForever:[self actionForKey:@"zombieAction"]]];

which will keep doing the action forever.

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