Create, configure, and run actions in SpriteKit.
You tell nodes to run an instance of
SKAction when you want to animate contents of your scene. When the scene processes frames of animation, the actions are executed. Some actions are completed in a single frame of animation, while other actions apply changes over multiple frames of animation before completing. The most common use for actions is to animate changes to a node’s properties. For example, you can create actions that move a node, scale or rotate it, or fade its transparency. However, actions can also change the node tree, play sounds, or even execute custom code.
Create and Run an Action
Do the following:
Control Action Timing
run(_:) an action, SpriteKit manages the interpolation. That means that SpriteKit changes the properties of the node over more than one frame rendered by the scene automatically for you. As a result, you control the timing of the animation by configuring:
Create a Reverse Action
Many actions can be reversed, allowing you to create another action object that reverses the effect of that action. For example, if an action object moves a node
20 points to the right of its current position, the reversed action moves the node
20 points to the left. To create a reversed action object, call an action object’s
Run Code when an Action Completes
run(_: method is identical to the
run(_:) method, but after the action completes, your block is called, but only if the action runs to completion. If the action is removed before it completes, the completion handler is never called. The following code fades out a sprite by running a
fade action on it and, after the action completes, presents a new scene:
After you create an action, its type cannot be changed, and you have a limited ability to change its properties. SpriteKit takes advantage of this immutable nature to execute the actions efficiently. Because actions are effectively immutable objects, you can run the same action safely on multiple nodes in the tree at the same time. For this reason, if you have an action that is used repeatedly in your game, create a single instance of the action and then reuse it whenever you need a node to execute it.
Drive Game Logic
Because actions can be chained and can wrap custom code, it's possible to drive most of your app's logic using a complex system of actions. But where actions leave more sophisticated behavior to be desired, you might need to override the
SKScene callbacks and drive node behavior from there. For example, if a node needs to be moved in a wholly custom manner, you can adjust its
update(_:) instead of using an action to do so.