I am pretty new to SpriteKit
. I have a set of nodes that need to move together to a different point for each node and after that animation completed for all of them I would like to do something else.
I was making this with UIView
components before. A [UIView animateWithDuration:completion:]
block was providing the thing that I needed. But in SpriteKit
each node has its own action and animate by itself. I could not find any block animation for sprite nodes. So I cannot control the animation completion.
Hope, I am clear. Thanks in advance.
There are a couple of SKAction class methods that may be of interest. The first being runBlock: and the second being group: (this allows actions to be run in parallel).
You can run actions on nodes with a completion handler using runAction: completion:
The following code puts each 'animation action', along with completion block in a 'block action'. The block actions are then put into a 'group action'. Finally the group action is told to run. This causes each block action to start at the same time. The completion handler of each block action calls a method named checkCompletion. This method checks for nodes (with a specific name property value) still animating by calling hasActions on the node. Because the node that called the checkCompletion method will return YES, if only one node returns YES then all animations are done.
The following demonstrates this using two nodes, however it will work for more than two.
checkCompletion method -
Unfortunately the following will not work as a block actions duration is instantaneous. Consequently the duration of the group action in this case is also instantaneous. This is the reason for the checkCompletion method.