BlueLettuce16
BlueLettuce16

Reputation: 2093

SpriteKit - how to add action to the node that will run after the previous action has finished

I have a node on which I run tow actions - one on the mouseDown event and second on the mouseUp event. The action that is being run may take longer than left mouse button is down and I would like to continue executing this action and somehow run the second action from the mouseUp method after the action from the mouseDown method has finished. Is it possible?

Upvotes: 0

Views: 1256

Answers (1)

Skyler Lauren
Skyler Lauren

Reputation: 3812

You don't have code for me to work off of and I am not that great with Swift anyway so hopefully this makes sense with Objective-C

First Option

OnMouseDown:

SKAction *firstAction = [SKAction moveByX:100 y:100 duration:5];
SKAction *customAction = [SKAction customActionWithDuration:0 actionBlock:^(SKNode *node, CGFloat elapsedTime) {
    if (self.runSecondAction)
    {
        SKAction *secondAction = [SKAction moveByX:-100 y:-100 duration:5];
        [node runAction:secondAction];
        self.runSecondAction = NO;
    }
}];

SKAction *squence = [SKAction sequence:@[firstAction, customAction]];

[sprite runAction:squence withKey:@"moveAction"];

OnMouseUp:

if ([sprite actionForKey:@"moveAction"])
{
    self.runSecondAction = YES;
}
else
{
    SKAction *secondAction = [SKAction moveByX:-100 y:-100 duration:5];
    [sprite runAction:secondAction];
}

The basic concept is you run a sequence with a custom action that runs a block. In that block you check to see if a bool has been set to run the second object. This will make it run after the first one is complete.

In mouseUp you want to run the action right away if the first action is done but if not you set a bool that will be check during the sequence.

Second Option

Don't run a sequence in OnMouseDown and check if there is a @"moveAction" going OnMouseUp. If there is check the state of the sprite and create the sequence at that point with what is remaining of that action. If there isn't that @"moveAction" just run the second action.

Hopefully that makes sense and is helpful.

Upvotes: 1

Related Questions