Class

SKConstraint

An object which constrains a node's position or rotation.

Overview

An SKConstraint object describes a mathematical constraint on a node’s position or orientation. Constraints are attached to nodes; after a scene processes any actions and physics interactions, it applies constraints attached to nodes in its node tree. Use constraints to ensure that certain relationships are true before a scene is rendered. For example, you might use a constraint to:

  • Change a node’s zRotation property so that it always points at another node or a position in the scene.

  • Keep a node within a specified distance of another node or a point in the scene.

  • Keep a node inside a specified rectangle.

  • Restrict the zRotation property of a node so that it has a more limited rotation range of motion.

To use constraints, create an NSArray object that contains one or more constraint objects and assign the array to a node’s constraints property. When the scene is evaluated, the constraints on a node are executed in the order they appear in the constraints array.

Once created, a constraint can’t be changed. However, you can selectively disable or enable a constraint by setting its enabled property. You can also use the referenceNode property to convert positions to the referenced coordinate system before applying the constraint.

Symbols

Creating Position Constraints

class func positionX(SKRange)

Creates a constraint that restricts the x-coordinate of a node’s position.

class func positionY(SKRange)

Creates a constraint that restricts the y-coordinate of a node’s position.

class func positionX(SKRange, y: SKRange)

Creates a constraint that restricts both coordinates of a node’s position.

Creating Orientation Constraints

class func orient(to: SKNode, offset: SKRange)

Creates a constraint that forces a node to rotate to face another node.

class func orient(to: CGPoint, offset: SKRange)

Creates a constraint that forces a node to rotate to face a fixed point.

class func orient(to: CGPoint, in: SKNode, offset: SKRange)

Creates a constraint that forces a node to rotate to face a point in another node’s coordinate system.

class func zRotation(SKRange)

Creates a constraint that limits the orientation of a node.

Creating Distance Constraints

class func distance(SKRange, to: SKNode)

Creates a constraint that keeps a node within a certain distance of another node.

class func distance(SKRange, to: CGPoint)

Creates a constraint that keeps a node within a certain distance of a point.

class func distance(SKRange, to: CGPoint, in: SKNode)

Creates a constraint that keeps a node within a certain distance of a point in another node’s coordinate system.

Determining the Coordinate System That a Constraint Is Applied In

var referenceNode: SKNode?

The node whose coordinate system should be used to apply the constraint.

Enabling a Constraint

var enabled: Bool

A Boolean value that specifies whether the constraint is applied.

Relationships

Inherits From