1

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.

Leonid Glanz
  • 1,261
  • 2
  • 16
  • 36
Ady Năstase
  • 249
  • 2
  • 9

1 Answers1

1

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.

ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
  • Actually my post has a kind of a leak. Well, those two actions specified as node1.runAction1 and the next one are in separate functions. In fact I am calling func1 - animateTheFirstBall() and func2 - animateTheSecondBall() instead of those node1.runAction1 ant the other. – Ady Năstase May 15 '15 at 20:55
  • Yes, please. I could very much appreciate a brief example as I am not using closures (that's what you are speaking of, I guess) quite often. Thanks for the effort. – Ady Năstase May 15 '15 at 21:07
  • Yes, it works great. That is ending my long array of sleepless nights. Thanks a lot again. – Ady Năstase May 15 '15 at 21:13
  • Glad I could help! Good luck with your project! Also, you mentioned you're unfamiliar with closures - I would really recommend looking into them if you've got some time, since they ate useful in so many places :) Apple's *The Swift Programming Language* covers them in detail. – ABakerSmith May 15 '15 at 21:15