Class

SKAction

An object that is executed by an SKNode to change its structure or content.

Overview

An SKAction object is an action that is executed by a node in the scene (SKScene). Actions are most often used to change the structure and content of the node to which they are attached but can also make other changes to the scene. When the scene processes its nodes, actions associated with those nodes are evaluated.To create an action, call the class method for the action you are interested in. Then, configure the action’s properties. Finally, to execute the action, call a node object’s run(_:) method (or a similar method on the SKNode class) and pass it the action object.

Most actions allow you to change a node’s properties, such as its position, rotation, or scale. Some actions specifically apply only to SKSpriteNode objects, allowing you to animate a sprite’s color or texture properties. Many of these actions are animated by SpriteKit, meaning that they change the properties of the associated node over more than one frame of animation rendered by the scene. When an action is animated, the duration property states how long that action takes to complete in seconds and its timingMode property defines the rate at which the animation executes. The action’s speed property allows you to adjust the timing of the animation by increasing or decreasing its playback speed.

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 reversed() method.

Some actions include other actions as children:

  • A sequence action has multiple child actions. Each action in the sequence begins after the previous action ends.

  • A group action has multiple child actions. All actions stored in the group begin executing at the same time.

  • A repeating action stores a single child action. When the child action completes, it is restarted.

Groups, sequences, and repeating actions can be nested. The ability to combine actions together allows you to add very sophisticated behaviors to a node.

Adding Actions to Nodes

Actions Are Self-Contained Objects

Every action is an opaque object that describes a change you want to make to the scene. All actions are implemented by the SKAction class; there are no visible subclasses. Instead, actions of different types are instantiated using class methods. For example, here are the most common things you use actions to do:

  • Changing a node’s position and orientation.

  • Changing a node’s size or scaling properties.

  • Changing a node’s visibility or making it translucent.

  • Changing a sprite node’s contents so that it animates through a series of textures.

  • Colorizing a sprite node.

  • Playing simple sounds.

  • Removing a node from the node tree.

  • Calling a block.

  • Invoking a selector on an object.

After you create an action, its type cannot be changed, and you have a limited ability to change its properties. SpriteKit takes advantage of the immutable nature of actions to execute them very efficiently.

Actions can either be instantaneous or non-instantaneous:

  • An instantaneous action starts and completes in a single frame of animation. For example, an action to remove a node from its parent is an instantaneous action because a node can’t be partially removed. Instead, when the action executes, the node is removed immediately.

  • A non-instantaneous action has a duration over which it animates its effects. When executed, the action is processed in each frame of animation until the action completes.

Nodes Run Actions

An action is only executed after you tell a node to run it. The simplest way to run an action is to call the node’s run(_:) method. Listing 3 creates a new move action and then tells the node to execute it.

let moveNodeUp = SKAction.moveBy(x: 0.0,
                                 y: 100.0,
                                 duration: 1.0)
rocketNode.run(moveNodeUp)

A move action has a duration, so this action is processed by the scene over multiple frames of animation until the elapsed time exceeds the duration of the action. After the animation completes, the action is removed from the node.

You can run actions at any time. However, if you add actions to a node while the scene is processing actions, the new actions may not execute until the following frame. The steps a scene uses to process actions are described in more detail in SKScene class reference.

A node can run multiple actions simultaneously, even if those actions were executed at different times. The scene keeps track of how far each action is from completing and computes the effect that the action has on the node. For example, if you run two actions that move the same node, both actions apply changes to every frame. If the move actions were in equal and opposite directions, the node would remain stationary.

Because action processing is tied to the scene, actions are processed only when the node is part of a presented scene’s node tree. You can take advantage of this feature by creating a node and assigning actions to it, but waiting until later to add the node to the scene. Later, when the node is added to the scene, it begins executing its actions immediately. This pattern is particularly useful because the actions that a node is running are copied and archived when the node is copied.

If a node is running any actions, its hasActions() method returns true.

Canceling Running Actions

To cancel actions that a node is running, call its removeAllActions() method. All actions are removed from the node immediately. If a removed action had a duration, any changes it already made to the node remain intact, but further changes are not executed.

Receiving a Callback when an Action Completes

The run(_:completion:) method is identical to the run(_:) method, but after the action completes, your block is called. This callback is only called 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 fadeOut(withDuration:) action on a sprite and, after the action completes, presents a new scene:

let fadeOut = SKAction.fadeOut(withDuration:2)
node.run(fadeOut) {
    skView.presentScene(newScene)
}

Using Named Actions for Precise Control over Actions

Normally, you can’t see which actions a node is executing and if you want to remove actions, you must remove all of them. If you need to see whether a particular action is executing or remove a specific action, you must use named actions. A named action uses a unique key name to identify the action. You can start, remove, find, and replace named actions on a node.

Listing 5 creates and runs a new action identified with the key, ignition.

let moveNodeUp = SKAction.moveBy(x: 0.0,
                                 y: 100.0,
                                 duration: 1.0)
rocketNode.run(moveNodeUp,
               withKey: "ignition")

The following key-based methods are available:

  • run(_:withKey:) method to run the action. If an action with the same key is already executing, it is removed before the new action is added.

  • action(forKey:) method to determine if an action with that key is already running.

  • removeAction(forKey:) method to remove the action.

shows how you might use a named action to control a sprite’s movement. When the user clicks inside the scene, the method is invoked. The code determines where the click occurred and then tells the sprite to run an action to move to that position. The duration is calculated ahead of time so that the sprite always appears to move at a fixed speed. Because this code uses the run(_:withKey:) method, if the sprite was already moving, the previous move is stopped mid-stream and the new action moves from the current position to the new position.

    override func mouseDown(with event: NSEvent) {
        guard let playerNodeParent = self.playerNode.parent else {
            return
        }
             
        let clickPoint = event.location(in: playerNodeParent)
        let charPos = playerNode.position
        let distance = hypot(clickPoint.x-charPos.x,
                             clickPoint.y-charPos.y)
        let moveToClick = SKAction.move(to: clickPoint,
                                        duration: TimeInterval(distance / characterSpeed))
        self.playerNode.run(moveToClick, withKey: "moveToClick")

    }

Actions That Run Other Actions

SpriteKit provides many standard action types that change the properties of nodes in your scene. But actions show their real power when you combine them. By combining actions, you can create complex and expressive animations that are still executed by running a single action. A compound action is as easy to work with as any of the basic action types. With that in mind, it is time to learn about sequences, groups, and repeating actions.

  • A sequence action (or sequence) has multiple child actions. Each action in the sequence begins after the previous action ends.

  • A group action (or group) has multiple child actions. All actions stored in the group begin executing at the same time.

  • A repeating action has a single child action. When the child action completes, it is restarted.

Sequences Run Actions in Series

A sequence is a set of actions that run consecutively. When a node runs a sequence, the actions are triggered in consecutive order. When one action completes, the next action starts immediately. When the last action in the sequence completes, the sequence action also completes.

The following code shows that a sequence is created using an array of other actions.

let moveUp = SKAction.moveBy(x: 0,
                             y: 100,
                             duration: 1.0)
let zoom = SKAction.scale(to: 2.0,
                          duration: 0.25)
let wait = SKAction.wait(forDuration: 0.5)
let fadeAway = SKAction.fadeOut(withDuration: 0.25)
let removeNode = SKAction.removeFromParent()
let sequence = SKAction.sequence([moveUp, zoom, wait, fadeAway, removeNode])
let node = SKNode()
node.run(sequence)
There are a few things worth noting in this example:

  • The wait action is a special action that is usually used only in sequences. This action simply waits for a period of time and then ends, without doing anything; you use them to control the timing of a sequence.

  • The removeNode action is an instantaneous action, so it takes no time to execute. You can see that although this action is part of the sequence, it does not appear on the timeline below. As an instantaneous action, it begins and completes immediately after the fade action completes. This action ends the sequence.

Figure 3

Move and zoom sequence timeline

Move and zoom sequence timeline

Groups Run Actions in Parallel

A group action is a collection of actions that all start executing as soon as the group is executed. You use groups when you want actions to be synchronized. For example, the following code rotates and turns a sprite to give the illusion of a wheel rolling across the screen. Using a group (rather than running two separate actions) emphasizes that the two actions are closely related.

let wheel = scene.childNode(withName: "wheel") as! SKSpriteNode
let circumference = wheel.size.height * CGFloat.pi
let oneRevolution = SKAction.rotate(byAngle: -CGFloat.pi * 2,
                                    duration: 2.0)
let moveRight = SKAction.moveBy(x: circumference,
                                y: 0, duration: 2.0)
let group = SKAction.group([oneRevolution, moveRight])
wheel.run(group)

Although the actions in a group start at the same time, the group does not complete until the last action in the group has finished running. The following code shows a more complex group that includes actions with different timing values. The sprite animates through its textures and moves down the screen for a period of two seconds. However, during the first second, the the sprite zooms in and changes from full transparency to a solid appearance. The following code shows that the two actions that make the sprite appear finish halfway through the group’s animation. The group continues until the other two actions complete.

sprite.setScale(0)
let animate = SKAction.animate(with: textures,
                               timePerFrame: 2 / TimeInterval(textures.count))
let moveDown = SKAction.moveBy(x: 0,
                               y: -200,
                               duration: 2)
let scale = SKAction.scale(to: 1,
                           duration: 1)
let fadeIn = SKAction.fadeIn(withDuration: 1)
let group = SKAction.group([moveDown, scale, fadeIn])
sprite.run(group)
Figure 4

Grouped actions start at the same time, but complete independently

Grouped actions start at the same time, but complete independently
Repeating Actions Execute Another Action Multiple Times

A repeating action loops another action so that it repeats multiple times. When a repeating action is executed, it executes its contained action. Whenever the looped action completes, it is restarted by the repeating action. The following code shows the creation methods used to create repeating actions. You can create an action that repeats an action a finite number of times or an action that repeats an action indefinitely.

Listing 10

Creating a repeating action

let fadeOut = SKAction.fadeOut(withDuration: 1)
let fadeIn = SKAction.fadeOut(withDuration: 1)
let pulse = SKAction.sequence([fadeOut, fadeIn])
let pulseThreeTimes = SKAction.repeat(pulse,
                                      count: 3)
let pulseForever = SKAction.repeatForever(pulse)

Figure 5 shows the timing arrangement for the pulseThreeTimes action. You can see that the sequence finishes, then repeats.

Figure 5

Timing arrangement for repeating action

Timing arrangement for repeating action

When you repeat a group, the entire group must finish before the group is restarted. Listing 11 creates a group that moves a sprite and animates its textures, but in this example the two actions have different durations. Figure 6 shows the timing diagram when the group is repeated. You can see that the texture animation runs to completion and then no animation occurs until the group repeats.

Listing 11

Creating a grouped action

let animate = SKAction.animate(with: textures,
                               timePerFrame: 2 / TimeInterval(textures.count))
let moveDown = SKAction.moveBy(x: 0,
                               y: -200,
                               duration: 2)
let group = SKAction.group([animate, moveDown])
Figure 6

Timing arrangement for grouped action

Timing arrangement for grouped action

What you may have wanted was for each action to repeat at its own natural frequency. To do this, create a set of repeating actions and then group them together. Listing 12 shows how you would implement the timing shown in the figure below.

Listing 12

Creating a repeating grouped action

let animate = SKAction.animate(with: textures,
                               timePerFrame: 1 / TimeInterval(textures.count))
let moveDown = SKAction.moveBy(x: 0,
                               y: -200,
                               duration: 2)
let repeatAnimation = SKAction.repeatForever(animate)
let repeatMove = SKAction.repeatForever(moveDown)
let group = SKAction.group([repeatAnimation, repeatMove])
Figure 7

Timing arrangement for repeating grouped action

Timing arrangement for repeating grouped action
Configuring Action Timing

By default, an action with a duration applies its changes linearly over the duration you specified. However, you can adjust the timing of animations through a few properties:

  • Normally, an animated action runs linearly. You can use an action’s timingMode property to choose a nonlinear timing mode for an animation. For example, you can have the action start quickly and then slow down over the remainder of the run.

  • An action’s speed property changes the rate at which an animation plays. You can speed up or slow down an animation from its default timing.

  • A speed value of 1.0 is the normal rate.

  • If you set an action’s speed property to 2.0, when the action is executed by a node, it plays twice as fast. To pause the action, set the value to 0. If you adjust the speed of an action that contains other actions (such as a group, sequence, or repeating action), the rate is applied to the actions contained within. The enclosed actions are also affected by their own speed property.

  • A node’s speed property has the same effect as the action’s speed property, but the rate is applied to all actions processed by the node or by any of the node’s descendants in the scene tree.

SpriteKit determines the rate at which an animation applies by finding all of the rates that apply to the action and multiplying them.

Tips for Working with Actions

Actions work best when you create them once and use them multiple times. Whenever possible, create actions early and save them in a location where they can be easily retrieved and executed.

Depending on the kind of action, any of the following locations might be useful:

  • A node’s userData property

  • The parent node’s userData property, if dozens of nodes share the same actions and the same parent

  • The scene’s userData property for actions shared by multiple nodes throughout the scene

  • If subclassing, then on a property of the subclass

If you need designer or artist input on how a node’s properties are animated, consider moving the action creation code into your custom design tools. Then archive the action and load it in your game engine.

When You Shouldn’t Use Actions

Although actions are efficient, there is a cost to creating and executing them. If you are making changes to a node’s properties in every frame of animation and those changes need to be recomputed in each frame, you are better off making the changes to the node directly and not using actions to do so.

Configuring Action Timing

By default, an action with a duration applies its changes linearly over the duration you specified. However, you can adjust the timing of animations through a few properties:

  • Normally, an animated action runs linearly. You can use an action’s timingMode property to choose a nonlinear timing mode for an animation. For example, you can have the action start quickly and then slow down over the remainder of the run (see SKActionTimingMode).

  • For fine control over an action's timing, you can create a custom timing function. See timingFunction.

  • An action’s speed property changes the rate at which an animation plays. You can speed up or slow down an animation from its default timing. A speed value of 1.0 is the normal rate. If you set an action’s speed property to 2.0, when the action is executed by a node, it plays twice as fast. To pause the action, set the value to 0. If you adjust the speed of an action that contains other actions (such as a group, sequence, or repeating action), the rate is applied to the actions contained within. The enclosed actions are also affected by their own speed property.

  • A node’s speed property has the same effect as the action’s speed property, but the rate is applied to all actions processed by the node or by any of the node’s descendants in the scene tree.

SpriteKit determines the rate at which an animation applies by finding all of the rates that apply to the action and multiplying them.

Observing Changes to Node Properties

Generally, actions do not call public methods on nodes. For example, if you wanted to subclass SKNode to respond to a move(to:duration:) action, you may consider overriding its position property to add a didSet observer (see Overriding Property Observers).

class MovingNode: SKSpriteNode {
    override var position: CGPoint {
        didSet {
            // code to react to position change
        }
    }
}

However, because a move action running on an instance of MovingNode doesn't set its position, the observer isn't invoked and your code to react to a position change is never executed.

In this example, the solution is to use SKSceneDelegate. By comparing a node's position in the delegate's update(_:for:) method - which is called at the beginning of each frame - to its position in the delegate's didEvaluateActions(for:) method - which is called after any actions have been evaluated - you can check if it has moved and react accordingly.

Listing 7 shows an example of how you can implement this solution.

Listing 7

Responding to a position change during a running action

let node = SKNode()
var nodePosition = CGPoint()
     
func update(_ currentTime: TimeInterval, for scene: SKScene) {
    nodePosition = node.position
}
     
func didEvaluateActions(for scene: SKScene) {
    let distance = hypot(node.position.x - nodePosition.x,
                         node.position.y - nodePosition.y)
    
    if distance > 0 {
        // code to react to position change
    }
}

Working with Inverse Kinematics

Inverse kinematics (IK) is the use of equations to drive a system of connected components so that an "end effector" can reach a desired position. Imagine a robot arm formed of a series of sections hinged together. With traditional animation techniques, if you wanted the "hand" of the arm to reach to a specified position, you would have to code the rotations for each of the joints -which is a complex and time consuming task.

With IK, you simply need to specify the required position of hand (which would be the end effector) and each individual joint is rotated to achieve the final position. In SpriteKit, complex IK animations can be generated with very little code. SpriteKit's IK relies on two classes:

  • SKAction object's reach methods that solve an IK system (i.e. that run the system to make the end effector reach a specified position).

  • SKConstraint objects that constrain the position or orientation of nodes.

Listing 1 creates a simple robot arm consisting of a fixed shoulder node and upperArm, midArm and lowerArm sections that are joined by elbow and wrist nodes. At the end of the wrist is an endEffector node.The nodes are progressively children of each other.

The shoulder node is fixed to the scene with a constraint and the other joints are offset in the y direction by the length of each section.

let sectionLength: CGFloat = 100
let sectionRect = CGRect(x: -10, y: -sectionLength,
                         width: 20, height: sectionLength)
   
let upperArm = SKShapeNode(rect: sectionRect)
let midArm = SKShapeNode(rect: sectionRect)
let lowerArm = SKShapeNode(rect: sectionRect)
let shoulder = SKShapeNode(circleOfRadius: 5)
let elbow = SKShapeNode(circleOfRadius: 5)
let wrist = SKShapeNode(circleOfRadius: 5)
let endEffector = SKShapeNode(circleOfRadius: 5)
   
scene.addChild(shoulder)
shoulder.addChild(upperArm)
upperArm.addChild(elbow)
elbow.addChild(midArm)
midArm.addChild(wrist)
wrist.addChild(lowerArm)
lowerArm.addChild(endEffector)
    
shoulder.constraints = [SKConstraint.positionX(SKRange(constantValue: 320),
                                               y: SKRange(constantValue: 320))]
    
let positionConstraint = SKConstraint.positionY(SKRange(constantValue: -sectionLength))
elbow.constraints =  [ positionConstraint ]
wrist.constraints = [ positionConstraint ]
endEffector.constraints = [ positionConstraint ]

In its default position, the code above creates a node tree that looks like this (the shoulder is colored in blue and the end effector is yellow).

Figure 1

Robot arm in rest position

Robot arm in rest position

To run the IK solver, you create an action passing it the desired location.

Listing 2

Running an IK solver

let reachAction = SKAction.reach(to: location,
                                 rootNode: shoulder,
                                 duration: 1.0)
     
endEffector.run(reachAction)

Given a location to the left and slightly below the shoulder, the action finishes with the node tree looking like Figure 2.

Figure 2

Robot arm after IK solve

Robot arm after IK solve

Subclassing Notes

You never subclass SKAction directly. Instead, create actions that call methods on arbitrary objects or execute blocks of code. See Creating Custom Actions.

Topics

Creating Actions That Move Nodes

class func moveBy(x: CGFloat, y: CGFloat, duration: TimeInterval)

Creates an action that moves a node relative to its current position.

class func move(by: CGVector, duration: TimeInterval)

Creates an action that moves a node relative to its current position.

class func move(to: CGPoint, duration: TimeInterval)

Creates an action that moves a node to a new position.

class func moveTo(x: CGFloat, duration: TimeInterval)

Creates an action that moves a node horizontally.

class func moveTo(y: CGFloat, duration: TimeInterval)

Creates an action that moves a node vertically.

class func follow(CGPath, duration: TimeInterval)

Creates an action that moves the node along a relative path, orienting the node to the path.

class func follow(CGPath, speed: CGFloat)

Creates an action that moves the node along a relative path at a specified speed, orienting the node to the path.

class func follow(CGPath, asOffset: Bool, orientToPath: Bool, speed: CGFloat)

Creates an action that moves the node at a specified speed along a path.

Creating Actions That Rotate Nodes

class func rotate(byAngle: CGFloat, duration: TimeInterval)

Creates an action that rotates the node by a relative value.

class func rotate(toAngle: CGFloat, duration: TimeInterval)

Creates an action that rotates the node counterclockwise to an absolute angle.

class func rotate(toAngle: CGFloat, duration: TimeInterval, shortestUnitArc: Bool)

Creates an action that rotates the node to an absolute value.

Creating Actions That Change a Node’s Animation Speed

class func speed(by: CGFloat, duration: TimeInterval)

Creates an action that changes how fast the node executes actions by a relative value.

class func speed(to: CGFloat, duration: TimeInterval)

Creates an action that changes how fast the node executes actions.

Creating Actions That Change a Node’s Scale

class func scale(by: CGFloat, duration: TimeInterval)

Creates an action that changes the x and y scale values of a node by a relative value.

class func scale(to: CGSize, duration: TimeInterval)

Creates an action that changes the x and y scale values of a node to achieve

class func scale(to: CGFloat, duration: TimeInterval)

Creates an action that changes the x and y scale values of a node.

class func scaleX(by: CGFloat, y: CGFloat, duration: TimeInterval)

Creates an action that adds relative values to the x and y scale values of a node.

class func scaleX(to: CGFloat, y: CGFloat, duration: TimeInterval)

Creates an action that changes the x and y scale values of a node.

class func scaleX(to: CGFloat, duration: TimeInterval)

Creates an action that changes the x scale value of a node to a new value.

class func scaleY(to: CGFloat, duration: TimeInterval)

Creates an action that changes the y scale value of a node to a new value.

Creating Actions to Show or Hide a Node

class func unhide()

Creates an action that makes a node visible.

class func hide()

Creates an action that hides a node.

Creating Actions That Change a Node’s Transparency

class func fadeIn(withDuration: TimeInterval)

Creates an action that changes the alpha value of the node to 1.0.

class func fadeOut(withDuration: TimeInterval)

Creates an action that changes the alpha value of the node to 0.0.

class func fadeAlpha(by: CGFloat, duration: TimeInterval)

Creates an action that adjusts the alpha value of a node by a relative value.

class func fadeAlpha(to: CGFloat, duration: TimeInterval)

Creates an action that adjusts the alpha value of a node to a new value.

Creating Actions That Change a Sprite Node’s Content

class func resize(byWidth: CGFloat, height: CGFloat, duration: TimeInterval)

Creates an action that adjusts the size of a sprite.

class func resize(toHeight: CGFloat, duration: TimeInterval)

Creates an action that changes the height of a sprite to a new absolute value.

class func resize(toWidth: CGFloat, duration: TimeInterval)

Creates an action that changes the width of a sprite to a new absolute value.

class func resize(toWidth: CGFloat, height: CGFloat, duration: TimeInterval)

Creates an action that changes the width and height of a sprite to a new absolute value.

class func setTexture(SKTexture)

Creates an action that changes a sprite’s texture.

class func setTexture(SKTexture, resize: Bool)

Creates an action that changes a sprite’s texture, possibly resizing the sprite.

class func animate(with: [SKTexture], timePerFrame: TimeInterval)

Creates an action that animates changes to a sprite’s texture.

class func animate(with: [SKTexture], timePerFrame: TimeInterval, resize: Bool, restore: Bool)

Creates an action that animates changes to a sprite’s texture, possibly resizing the sprite.

class func setNormalTexture(SKTexture)

Creates an action that changes a sprite’s normal texture.

class func setNormalTexture(SKTexture, resize: Bool)

Creates an action that changes a sprite’s normal texture, possibly resizing the sprite.

class func animate(withNormalTextures: [SKTexture], timePerFrame: TimeInterval)

Creates an action that animates changes to a sprite’s normal texture.

class func colorize(with: UIColor, colorBlendFactor: CGFloat, duration: TimeInterval)

Creates an animation that animates a sprite’s color and blend factor.

class func colorize(withColorBlendFactor: CGFloat, duration: TimeInterval)

Creates an action that animates a sprite’s blend factor.

Creating Physics Actions

class func applyForce(CGVector, duration: TimeInterval)

Creates an action that applies a force to the center of gravity of a node’s physics body.

class func applyTorque(CGFloat, duration: TimeInterval)

Creates an action that applies a torque to an node’s physics body.

class func applyForce(CGVector, at: CGPoint, duration: TimeInterval)

Creates an action that applies an force to a specific point on a node’s physics body.

class func applyImpulse(CGVector, duration: TimeInterval)

Creates an action that applies an impulse to the center of gravity of a physics body.

class func applyAngularImpulse(CGFloat, duration: TimeInterval)

Creates an action that applies an angular impulse to a node’s physics body.

class func applyImpulse(CGVector, at: CGPoint, duration: TimeInterval)

Creates an action that applies an impulse to a specific point of a node’s physics body.

class func changeCharge(to: Float, duration: TimeInterval)

Creates an action that changes the charge of a node’s physics body to a new value.

class func changeCharge(by: Float, duration: TimeInterval)

Creates an action that changes the charge of a node’s physics body by a relative value.

class func changeMass(to: Float, duration: TimeInterval)

Creates an action that changes the mass of a node’s physics body to a new value.

class func changeMass(by: Float, duration: TimeInterval)

Creates an action that changes the mass of a node’s physics body by a relative value.

class func strength(to: Float, duration: TimeInterval)

Creates an action that animates a change of a physics field’s strength.

class func strength(by: Float, duration: TimeInterval)

Creates an action that animates a change of a physics field’s strength to a value relative to the existing value.

class func falloff(to: Float, duration: TimeInterval)

Creates an action that animates a change of a physics field’s falloff.

class func falloff(by: Float, duration: TimeInterval)

Creates an action that animates a change of a physics field’s falloff to a value relative to the existing value.

Creating Actions to Warp a Node

class func animate(withWarps: [SKWarpGeometry], times: [NSNumber])

Creates an action to distort a node through a sequence of SKWarpGeometry objects.

class func animate(withWarps: [SKWarpGeometry], times: [NSNumber], restore: Bool)

Creates an action to distort a node through a sequence of SKWarpGeometry objects.

class func warp(to: SKWarpGeometry, duration: TimeInterval)

Creates an action to distort a node based using an SKWarpGeometry object.

Creating Audio Actions

class func play()

Creates an action that tells an audio node to start playback.

class func pause()

Creates an action that tells an audio node to pause playback.

class func stop()

Creates an action that tells an audio node to stop playback.

class func changePlaybackRate(to: Float, duration: TimeInterval)

Creates an action that changes an audio node’s playback rate to a new value.

class func changePlaybackRate(by: Float, duration: TimeInterval)

Creates an action that changes an audio node’s playback rate by a relative amount.

class func changeVolume(to: Float, duration: TimeInterval)

Creates an action that changes an audio node’s volume to a new value.

class func changeVolume(by: Float, duration: TimeInterval)

Creates an action that changes an audio node’s volume by a relative value.

class func changeObstruction(to: Float, duration: TimeInterval)

Creates an action that changes an audio node’s obstruction to a new value.

class func changeObstruction(by: Float, duration: TimeInterval)

Creates an action that changes an audio node’s obstruction by a relative value.

class func changeOcclusion(to: Float, duration: TimeInterval)

Creates an action that changes an audio node’s occlusion to a new value.

class func changeOcclusion(by: Float, duration: TimeInterval)

Creates an action that changes an audio node’s occlusion by a relative value.

class func changeReverb(to: Float, duration: TimeInterval)

Creates an action that changes an audio node’s reverb to a new value.

class func changeReverb(by: Float, duration: TimeInterval)

Creates an action that changes an audio node’s reverb by a relative value.

class func stereoPan(to: Float, duration: TimeInterval)

Creates an action that changes an audio node’s stereo panning to a new value.

class func stereoPan(by: Float, duration: TimeInterval)

Creates an action that changes an audio node’s stereo panning by a relative value.

Removing Nodes from the Scene

class func removeFromParent()

Creates an action that removes the node from its parent.

Creating Actions That Perform Actions on a Node’s Child

class func run(SKAction, onChildWithName: String)

Creates an action that runs an action on a named child object.

Creating Actions That Combine or Repeat Other Actions

class func group([SKAction])

Creates an action that runs a collection of actions in parallel.

class func sequence([SKAction])

Creates an action that runs a collection of actions sequentially.

class func `repeat`(SKAction, count: Int)

Creates an action that repeats another action a specified number of times.

class func repeatForever(SKAction)

Creates an action that repeats another action forever.

Creating an Action to Introduce a Delay into a Sequence

class func wait(forDuration: TimeInterval)

Creates an action that idles for a specified period of time.

class func wait(forDuration: TimeInterval, withRange: TimeInterval)

Creates an action that idles for a randomized period of time.

Creating Actions to Perform Inverse Kinematic Calculations

class func reach(to: CGPoint, rootNode: SKNode, duration: TimeInterval)

Creates an action that performs an inverse kinematic reach.

class func reach(to: CGPoint, rootNode: SKNode, velocity: CGFloat)

Creates an action that performs an inverse kinematic reach.

class func reach(to: SKNode, rootNode: SKNode, duration: TimeInterval)

Creates an action that performs an inverse kinematic reach.

class func reach(to: SKNode, rootNode: SKNode, velocity: CGFloat)

Creates an action that performs an inverse kinematic reach.

Creating Custom Actions

init?(named: String)

Creates an action of the given name from an action file.

init?(named: String, duration: TimeInterval)

Creates an action of the given name from an action file with a new duration.

init?(named: String, from: URL)

Creates an action of the given name from an action file.

init?(named: String, from: URL, duration: TimeInterval)

Creates an action of the given name from an action file with a new duration.

class func perform(Selector, onTarget: Any)

Creates an action that calls a method on an object.

class func run(() -> Void)

Creates an action that executes a block.

class func run(() -> Void, queue: DispatchQueue)

Creates an action that executes a block on a specific dispatch queue.

Reversing an Action

func reversed()

Creates an action that reverses the behavior of another action.

Inspecting an Action’s Animation Properties

var speed: CGFloat

A speed factor that modifies how fast an action runs.

var timingMode: SKActionTimingMode

The timing mode used to execute an action.

var timingFunction: SKActionTimingFunction

A block used to customize the timing function.

var duration: TimeInterval

The duration required to complete an action.

Constants

enum SKActionTimingMode

The modes that an action can use to adjust the apparent timing of the action.

typealias SKActionTimingFunction

The signature for the custom timing block.

Relationships

Inherits From

See Also

Animating Content

class SKTransition

An SKTransition object is used to perform an animated transition between a SKScene object already presented by an SKView object and a new incoming scene.