Search code examples
swiftskactionsknode

Swift - How to run 2 actions non-simultaneously (in linear mode) for two different SKNodes in the same function


I have a function in which I need to run 2 actions one after another. e.g.

func foo()
{
     ..........
     if someConditions
     {
          node1.runAction1
          node2.runAction2
     }
}

It seems that swift is running those actions simultaneously.
And that's exactly what I do not want to happen in my game.
I want action2 to start after action1 is finished.
What should I have to do?
Many thanks.


Solution

  • You could pass a completion handler when calling node1.runAction that will start node2's action when node1's action is compelete. For example:

    node1.runAction(action1) {
        node2.runAction(action2)
    }
    

    Edit

    In response to your comment, here is a possible solution: Define runAction1 like so (I'm assuming runAction1 is a method on one of your classes).

    func runAction1(completion: () -> Void) {
        // ...
        self.runAction(action, completion: completion)
    }
    

    Then use this like so:

    node1.runAction1(completion: node2.runAction2)
    

    Hope that helps.