Search code examples
swiftsprite-kitgroupingskaction

Group several nodes for action and be notificated after completion


I have a SKNode with some children. For these children I'd like to apply actions which should start a the same time (sample code, k1000ms is a constant):

let children = self.children        
for var i=0; i<children.count; i++ {
  children[i].runAction(SKAction.moveTo(CGPoint(x: 0.0, y: 0.), duration: k1000ms))
}

The point I don't know how long an iteration takes so starting actions for a huge number of children might result in a delay between the first and last started action. The solution is obviously apply the actions to all nodes first and then starting the actions with a single call (hopefully the OS takes care of proper synchronization but this is an other issue).

Question 1: How to apply the actions to all nodes first and afterwards start all at once?

Question 2: How to get notified all actions are performed?


Solution

  • 1: You can't and needn't. All actions started (run) in the same frame with the same duration will end in the same frame. Your entire for loop is performed in a single frame (unless you are multithreading it). The next frame won't process (evaluate) actions and render until your for loop has ended.

    enter image description here

    2: Use a sequence action with a runblock action as the last action, or use the completion block.