iOS Developer Library

Developer

SpriteKit Framework Reference SKNode Class Reference

Options
Deployment Target:

On This Page
Language:

SKNode

Conforms To


Import Statement


Swift

import SpriteKit

Objective-C

@import SpriteKit;

Availability


Available in iOS 7.0 and later

The SKNode class is the fundamental building block of most Sprite Kit content. The SKNode class doesn’t draw any visual content. Its primary role is to provide baseline behavior that the other node classes use. All visual elements in a Sprite Kit-based game are drawn using predefined SKNode subclasses.

Table 1 describes the node subclasses provided in Sprite Kit.

Table 1Node subclasses

Class

Description

SKSpriteNode

A node that draws a textured sprite.

SKVideoNode

A node that plays video content.

SKLabelNode

A node that renders a text string.

SKShapeNode

A node that renders a shape based on a Core Graphics path.

SKEmitterNode

A node that creates and renders particles.

SKCropNode

A node that crops its child nodes using a mask.

SKEffectNode

A node that applies a Core Image filter to its child nodes.

SKLightNode

A node that is used to apply lighting and shadows to a scene.

SKFieldNode (and subclasses)

A node that applies physics effects to a portion of the scene.

Characteristics of Nodes

Nodes are organized hierarchically into node trees, similar to how views and subviews work. Most commonly, a node tree is defined with a scene node (SKScene) as the root node and other content nodes as descendants. The scene node runs an animation loop that processes actions on the nodes, simulates physics, and then renders the contents of the node tree for display.

Every node in a node tree provides a coordinate system to its children. After a child is added to the node tree, it is positioned inside its parent’s coordinate system by setting its position properties. A node’s coordinate system can be scaled and rotated by changing its xScale, yScale, and zRotation properties. When a node’s coordinate system is scaled or rotated, this transformation is applied both to the node’s own content and to that of its descendants.

The SKNode class does not perform any drawing of its own. However, many SKNode subclasses render visual content and so the SKNode class understands some visual concepts:

  • The frame property provides the bounding rectangle for a node’s visual content, modified by the scale and rotation properties. The frame is non-empty if the node’s class draws content. Each node subclass determines the size of this content differently. In some subclasses, the size of the node’s content is declared explicitly, such as in the SKSpriteNode class. In other subclasses, the content size is calculated implicitly by the class using other object properties. For example, an SKLabelNode object determines its content size using the label’s message text and font characteristics.

  • A node’s accumulated frame, retrieved by calling the calculateAccumulatedFrame method, is the largest rectangle that includes the frame of the node and the frames of all its descendants.

  • Other properties, such as the alpha and hidden properties, affect how the node and its descendants are drawn.

All nodes are responder objects that can respond directly to user interaction with the node onscreen. You can also convert between coordinate systems and perform hit testing to determine which nodes a point lies in. You can also perform intersections between nodes in the tree to determine if their physical areas overlap.

Any node in the tree may run actions, which are used to animate the properties of a node, add or remove nodes, play sounds, or perform other custom tasks. Actions are the heart of the animation system in Sprite Kit.

A node can support a physics body, which is an object that simulates the physical properties of the object. When a node has a physics body, the physics simulation automatically computes a new position for the physics body and then moves and rotates the node to match that position.

A node can supply constraints that express relationships with other nodes or locations in the scene. These constraints are automatically applied by the scene before the scene is rendered. Another set of constraints is used in conjunction with actions to perform inverse-kinematic animations.

Use Plain Nodes to Organize Your Game Content

Even though SKNode objects cannot directly draw content, there are useful ways to use them in your game. Here are some ideas to get you started:

  • You have content that is built up from multiple node objects, either sprites or other content nodes. However, you want this content to be thought of as a single object within your game, without promoting any one of the content nodes to be the root. A basic node is appropriate, because you can give it a position in the scene tree and then make all of the other nodes its descendants. These individual pieces can also be moved or adjusted relative to the parent’s location.

  • Use node objects to organize your drawn content into a series of layers. For example, many games have a background layer for the world, another layer for characters, and a third layer for text and other game information. Other games have many more layers. Create the layers as basic nodes, and insert them into the scene in order. Then, as necessary, you can make individual layers visible or invisible.

  • You need an object in the scene that is invisible but that performs some other necessary function. For example, in a dungeon exploring game, an invisible node might be used to represent a hidden trap. When another node intersects it, the trap is triggered. (See Searching for Physics Bodies.) Or for another example, you might add a node as a child of another node that represents the position of the player’s point of view. See Example: Centering the Scene on a Node.

There are advantages to having a node in the tree to represent these concepts:

  • You can add or remove entire subtrees by adding or removing a single node. This makes scene management efficient.

  • You can adjust properties of a node in the tree and have the effects of those properties propagate down to the node’s descendants. For example, if the basic node has sprite nodes as its children, rotating the basic node rotates all of the sprite content also.

  • You can take advantage of actions, physics contacts, and other Sprite Kit features to implement the concept.

Searching the Node Tree

The nodes in the scene tree are often organized to determine the precise rendering order for the scene, not the role these nodes play in your scene. In addition, you may also be loading nodes from an archive file rather than instantiating them directly at runtime. Because of this, you may need to be able to find specific nodes within a node tree. To do this, you provide names to nodes and then search for those names.

A node’s name property holds a node’s name. The name should be an alphanumeric string without any punctuation. Listing 1 shows how you might name three different nodes to distinguish them from each other.

Listing 1Naming a set of nodes
  • playerNode.name = @"player";
  • monsterNode1.name = @"goblin";
  • monsterNode2.name = @"ogre";

When you name nodes in the tree, decide whether those names will be unique. If a node’s name is unique, you should never include more than one node with that name in the scene tree. On the other hand, if a node name is not unique within your game, it might represent a collection of related nodes. For example, in Listing 1, there are probably multiple goblins within the game, and you might want to identify them all with the same name. But the player might be a unique node within the game.

The node name usually serves two purposes in your app:

  • You can write your own code that implements game logic based on the node’s name. For example, when two physics objects collide, you might use the node names to determine how the collision affects gameplay.

  • You can search for nodes that have a particular name. Typically, this is done once when a scene is first loaded.

The SKNode class implements the following methods for searching the node tree:

  • The childNodeWithName: method searches a node’s children until it finds a matching node, then it stops and returns this node. This method is usually used to search for nodes with unique names.

  • The enumerateChildNodesWithName:usingBlock: method searches a node’s children and calls your block once for each matching node it finds. You use this method when you want to find all nodes that share the same name.

  • The objectForKeyedSubscript: method returns an array of nodes that match a particular name.

Listing 2 shows how you might create a method on your scene class to find the player node. You might use a method like this inside your code that loads and prepares a scene.

Listing 2Finding the player node
  • - (SKNode *)playerNode
  • {
  • [return [self childNodeWithName:@"player"];
  • }

When this method is called on the scene, the scene searches its children (and only its children) for a node whose name property matches the search string, then returns the node. When specifying a search string, you can either specify the name of the node or a class name. For example, if you create your own subclass for the player node and name it PlayerSprite, then you could specify PlayerSprite as the search string instead of player; the same node would be returned.

Advanced Searches

The default search only searches a node’s children and must exactly match either the node’s name or its class. However, Sprite Kit provides an expressive search syntax so that you can perform more advanced searches. For example, you could do the same search as before but search the entire scene tree. Or you could search the node’s children, but match a pattern rather than requiring an exact match.

Table 2 describes the different syntax options. The search uses common regular expression semantics.

Table 2Search syntax options

Syntax

Description

/

When placed at the start of the search string, this indicates that the search should be performed on the tree’s root node.

//

When placed at the start of the search string, this specifies that the search should begin at the root node and be performed recursively across the entire node tree. It is not legal anywhere else in the search string.

..

The search should move up to the node’s parent.

/

When placed anywhere but the start of the search string, this indicates that the search should move to the node’s children.

*

The search matches zero or more characters.

[characters delimited by commas or dashes]

The search matches any of the characters contained inside the brackets.

alphanumeric characters

The search matches only the specified characters.

Table 3 shows some useful search strings to help get you started.

Table 3Example searches

Search string

Description

/MyName

This searches the root node’s children and matches any node with the name MyName.

//*

This search string matches every node in the node tree.

//MyName/..

This searches the node tree and matches the parent node of every node named MyName.

A[0-9]

This searches the node’s children and returns any child named A0, A1, …, A9.

Abby/Normal

This searches the node’s grandchildren and returns any node whose name is Normal and whose parent is named Abby.

//Abby/Normal

This searches the node tree and returns any node whose name is Normal and whose parent is named Abby.

Subclassing Notes

Subclassing the SKNode class is a useful way to build up more complex behaviors in your game. See Use Subclassing to Create Your Own Node Behaviors.

If you add properties to a subclass and that subclass needs to be archived, the NSCoding protocol needs to be implemented on your subclasses. See Archives and Serializations Programming Guide.

Unlike views, you cannot create SKNode subclasses that perform custom drawing. To implement this kind of behavior you need to work with existing classes in SpriteKit. For example, you might create a hierarchy of nodes that each perform some of the rendering behavior you need, or you might use node classes that support custom graphics shaders and implement your graphical effects in a shader.

  • Creates a new node.

    Declaration

    Objective-C

    + (instancetype)node

    Return Value

    A newly initialized node.

    Discussion

    The SKNode class does not draw or perform any tasks directly, so it is more common to instantiate a node subclass instead. However, you might create an SKNode object to organize other related nodes in the node tree.

    Import Statement

    Objective-C

    @import SpriteKit;

    Availability

    Available in iOS 7.0 and later

  • Creates a new node by loading an archive file from the game’s main bundle.

    Declaration

    Swift

    convenience init!(fileNamed filename: String)

    Objective-C

    + (instancetype)nodeWithFileNamed:(NSString *)filename

    Parameters

    filename

    The name of the file, without a file extension. The file must be in the app’s main bundle and have a .sks filename extension.

    Return Value

    The unarchived node object.

    Discussion

    If you call this method on a subclass of the SKScene class and the object in the archive is an SKScene object, the returned object is initialized as if it is a member of the subclass. You use this behavior to create scene layouts in the Xcode Editor and provide custom behaviors in your subclass.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later

  • frame frame Property

    A rectangle in the parent’s coordinate system that contains the node’s content, ignoring the node’s children. (read-only)

    Declaration

    Swift

    var frame: CGRect { get }

    Objective-C

    @property(nonatomic, readonly) CGRect frame

    Discussion

    The frame is the smallest rectangle that contains the node’s content, taking into account the node’s xScale, yScale, and zRotation properties. Not all nodes contain content of their own.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Calculates a rectangle in the parent’s coordinate system that contains the content of the node and all of its descendants.

    Declaration

    Swift

    func calculateAccumulatedFrame() -> CGRect

    Objective-C

    - (CGRect)calculateAccumulatedFrame

    Discussion

    The frame takes into the account the cumulative effect of the xScale, yScale, and zRotation properties of each node in the subtree.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

    See Also

    frame

  • position position Property

    The position of the node in its parent's coordinate system.

    Declaration

    Swift

    var position: CGPoint

    Objective-C

    @property(nonatomic) CGPoint position

    Discussion

    The default value is (0.0,0.0).

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • zPosition zPosition Property

    The height of the node relative to its parent.

    Declaration

    Swift

    var zPosition: CGFloat

    Objective-C

    @property(nonatomic) CGFloat zPosition

    Discussion

    The default value is 0.0. The positive z axis is projected toward the viewer so that nodes with larger z values are closer to the viewer. When a node tree is rendered, the height of each node (in absolute coordinates) is calculated and then all nodes in the tree are rendered from smallest z value to largest z value. If multiple nodes share the same z position, those nodes are sorted so that parent nodes are drawn before their children, and siblings are rendered in the order that they appear in their parent’s children array. Hit-testing is processed in the opposite order.

    The SKView class’s ignoresSiblingOrder property controls whether node sorting is enabled for nodes at the same z position.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

    See Also

    children

  • Sets the xScale and yScale properties of the node.

    Declaration

    Swift

    func setScale(_ scale: CGFloat)

    Objective-C

    - (void)setScale:(CGFloat)scale

    Parameters

    scale

    The new value to use for the node’s xScale and yScale properties.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • xScale xScale Property

    A scaling factor that multiplies the width of a node and its children.

    Declaration

    Swift

    var xScale: CGFloat

    Objective-C

    @property(nonatomic) CGFloat xScale

    Discussion

    The xScale property scales the width of the node and all of its descendants. The scale value affects how a node’s frame is calculated, its hit test area, how it is drawn, and other similar characteristics. The default value is 1.0.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • yScale yScale Property

    A scaling factor that multiplies the height of a node and its children.

    Declaration

    Swift

    var yScale: CGFloat

    Objective-C

    @property(nonatomic) CGFloat yScale

    Discussion

    The yScale property scales the height of the node and all of its descendants. The scale value affects how a node’s frame is calculated, its hit test area, how it is drawn, and other similar characteristics. The default value is 1.0.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • zRotation zRotation Property

    The Euler rotation about the z axis (in radians).

    Declaration

    Swift

    var zRotation: CGFloat

    Objective-C

    @property(nonatomic) CGFloat zRotation

    Discussion

    The default value is 0.0, which indicates no rotation. A positive value indicates a counterclockwise rotation. When the coordinate system is rotated, it affects the node and its descendants. The rotation affects the node’s frame property, hit testing, rendering, and other similar characteristics.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • alpha alpha Property

    The transparency value applied to the node’s contents.

    Declaration

    Swift

    var alpha: CGFloat

    Objective-C

    @property(nonatomic) CGFloat alpha

    Discussion

    The default value is 1.0.

    The SKNode class does not perform drawing, but many of its subclasses do. When a node or any of its descendants are drawn, the alpha component of each pixel is multiplied by the node’s alpha property and then clamped to the range 0.0-1.0. This modified alpha value is used to blend the pixel into the framebuffer. Subclasses that render content define properties that determine the blending operations used in conjunction with the alpha value to blend pixels into the parent’s framebuffer.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • hidden hidden Property

    A Boolean value that determines whether a node and its descendants are rendered.

    Declaration

    Swift

    var hidden: Bool

    Objective-C

    @property(nonatomic, getter=isHidden) BOOL hidden

    Discussion

    When hidden, a node and its descendants are not rendered. However, they still exist in the scene and continue to interact in other ways. For example, the node’s actions still run and the node can still be intersected with other nodes. The default value is NOfalse.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

    See Also

    paused

  • A Boolean value that indicates whether the node receives touch events.

    Declaration

    Swift

    var userInteractionEnabled: Bool

    Objective-C

    @property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled

    Discussion

    The default value is NOfalse.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Adds a node to the end of the receiver’s list of child nodes.

    Declaration

    Swift

    func addChild(_ node: SKNode)

    Objective-C

    - (void)addChild:(SKNode *)node

    Parameters

    node

    The node to add.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

    See Also

    children

  • Inserts a child into a specific position in the receiver’s list of child nodes.

    Declaration

    Swift

    func insertChild(_ node: SKNode!, atIndex index: Int)

    Objective-C

    - (void)insertChild:(SKNode *)node atIndex:(NSInteger)index

    Parameters

    node

    The node to add.

    index

    The position in the array to insert the node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

    See Also

    children

  • Removes the receiving node from its parent.

    Declaration

    Swift

    func removeFromParent()

    Objective-C

    - (void)removeFromParent

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

    See Also

    parent
    children

  • Removes all of the node’s children.

    Declaration

    Swift

    func removeAllChildren()

    Objective-C

    - (void)removeAllChildren

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

    See Also

    children

  • Removes a list of children from the receiving node.

    Declaration

    Swift

    func removeChildrenInArray(_ nodes: [AnyObject]!)

    Objective-C

    - (void)removeChildrenInArray:(NSArray *)nodes

    Parameters

    nodes

    An array of SKNode objects that are all children of the receiving node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

    See Also

    children

  • Returns a Boolean value that indicates whether the node is a descendant of the target node.

    Declaration

    Swift

    func inParentHierarchy(_ parent: SKNode) -> Bool

    Objective-C

    - (BOOL)inParentHierarchy:(SKNode *)parent

    Parameters

    parent

    An SKNode object to test against.

    Return Value

    YEStrue if the node is a descendant of the parent node; otherwise NOfalse.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • children children Property

    The node’s children. (read-only)

    Declaration

    Swift

    var children: [AnyObject] { get }

    Objective-C

    @property(nonatomic, readonly) NSArray *children

    Discussion

    The objects in this array are all SKNode objects.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • parent parent Property

    The node’s parent node. (read-only)

    Declaration

    Swift

    var parent: SKNode? { get }

    Objective-C

    @property(nonatomic, readonly) SKNode *parent

    Discussion

    If the node is not in a node tree, the value is nil.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • scene scene Property

    The scene node that contains the node. (read-only)

    Declaration

    Swift

    var scene: SKScene? { get }

    Objective-C

    @property(nonatomic, readonly) SKScene *scene

    Discussion

    If the node is not embedded in a scene, the value is nil.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • name name Property

    The node’s assignable name.

    Declaration

    Swift

    var name: String?

    Objective-C

    @property(nonatomic, copy) NSString *name

    Discussion

    This property is used to identify a node in other parts of your game logic. For example, you might use this name as part of collision testing. You can also search for nodes in a tree by their name.

    When choosing a name for a node, decide whether each node gets a unique name or whether some nodes will share a common name. If you give the node a unique name, you can find the node later by calling the childNodeWithName: method. If a name is shared by multiple nodes, the name usually means that these are all a similar object type in your game. In this case, you can iterate over those objects by calling the enumerateChildNodesWithName:usingBlock: method.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Searches the children of the receiving node for a node with a specific name.

    Declaration

    Swift

    func childNodeWithName(_ name: String) -> SKNode?

    Objective-C

    - (SKNode *)childNodeWithName:(NSString *)name

    Parameters

    name

    The name to search for. This may be either the literal name of the node or a customized search string. See Searching the Node Tree.

    Return Value

    If a node object with that name is found, the method returns the node object. Otherwise, it returns nil.

    Discussion

    If more than one child share the same name, the first node discovered is returned.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

    See Also

    name

  • Search the children of the receiving node to perform processing for nodes which share a name.

    Declaration

    Swift

    func enumerateChildNodesWithName(_ name: String, usingBlock block: ((SKNode!, UnsafeMutablePointer<ObjCBool>) -> Void)!)

    Objective-C

    - (void)enumerateChildNodesWithName:(NSString *)name usingBlock:(void (^)(SKNode *node, BOOL *stop))block

    Parameters

    name

    The name to search for. This may be either the literal name of the node or a customized search string. See Searching the Node Tree.

    block

    A block to execute on nodes that match the name parameter. The block takes the following arguments:

    node

    A node that matches the name.

    stop

    A pointer to a Boolean variable. Your block can set this to YEStrue to terminate the enumeration.

    Discussion

    This method enumerates the child array in order, searching for nodes whose names match the search parameter. The block is called once for each node that matches the name parameter.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

    See Also

    name

  • Returns an array of nodes that match the name parameter.

    Declaration

    Objective-C

    - (NSArray *)objectForKeyedSubscript:(NSString *)name

    Parameters

    name

    The name to search for. This may be either the literal name of the node or a customized search string. See Searching the Node Tree.

    Return Value

    An array of SKNode objects that match the name. If no matching nodes are found, an empty array is returned.

    Import Statement

    Objective-C

    @import SpriteKit;

    Availability

    Available in iOS 8.0 and later

  • Adds an action to the list of actions executed by the node.

    Declaration

    Swift

    func runAction(_ action: SKAction!)

    Objective-C

    - (void)runAction:(SKAction *)action

    Parameters

    action

    The action to perform.

    Discussion

    The new action is processed the next time the scene’s animation loop is processed.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Adds an action to the list of actions executed by the node. Your block is called when the action completes.

    Declaration

    Swift

    func runAction(_ action: SKAction!, completion block: (() -> Void)!)

    Objective-C

    - (void)runAction:(SKAction *)action completion:(void (^)(void))block

    Parameters

    action

    The action to perform.

    block

    A completion block called when the action completes.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Adds an identifiable action to the list of actions executed by the node.

    Declaration

    Swift

    func runAction(_ action: SKAction, withKey key: String!)

    Objective-C

    - (void)runAction:(SKAction *)action withKey:(NSString *)key

    Parameters

    action

    The action to perform.

    key

    A unique key used to identify the action.

    Discussion

    This method is identical to runAction:, but the action is stored so that it can be retrieved later. If an action using the same key is already running, it is removed before the new action is added.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Returns an action associated with a specific key.

    Declaration

    Swift

    func actionForKey(_ key: String) -> SKAction?

    Objective-C

    - (SKAction *)actionForKey:(NSString *)key

    Parameters

    key

    A string that uniquely identifies an action.

    Return Value

    If an action exists that matches the key, the action object is returned. Otherwise, nil is returned.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Returns a Boolean value that indicates whether the node is executing actions.

    Declaration

    Swift

    func hasActions() -> Bool

    Objective-C

    - (BOOL)hasActions

    Return Value

    YEStrue if the node has any executing actions; otherwise NOfalse.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Ends and removes all actions from the node.

    Declaration

    Swift

    func removeAllActions()

    Objective-C

    - (void)removeAllActions

    Discussion

    When an action is removed from the node, any remaining animation the action would perform is skipped; however, previous changes are not reverted. It is possible that an action may make a final change to the scene when removed; if so, it is documented for the specific action in SKAction Class Reference.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Removes an action associated with a specific key.

    Declaration

    Swift

    func removeActionForKey(_ key: String!)

    Objective-C

    - (void)removeActionForKey:(NSString *)key

    Parameters

    key

    A string that uniquely identifies an action.

    Discussion

    If an action is found that matches the key, it is removed from the node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • speed speed Property

    A speed modifier applied to all actions executed by a node and its descendants.

    Declaration

    Swift

    var speed: CGFloat

    Objective-C

    @property(nonatomic) CGFloat speed

    Discussion

    The default value is 1.0, which means that all actions run at their normal speed. If you set a different speed, time appears to run faster or slower for all actions executed on the node and its descendants. For example, if you set a speed value of 2.0, actions run twice as fast.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • paused paused Property

    A Boolean value that determines whether actions on the node and its descendants are processed.

    Declaration

    Swift

    var paused: Bool

    Objective-C

    @property(nonatomic, getter=isPaused) BOOL paused

    Discussion

    If the value is YEStrue, the node (and all of its descendants) are skipped when a scene processes actions.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • The physics body associated with the node.

    Declaration

    Swift

    var physicsBody: SKPhysicsBody?

    Objective-C

    @property(nonatomic, retain) SKPhysicsBody *physicsBody

    Discussion

    The default value is nil, which indicates that the node does not participate in the physics simulation at all. If a physics body is provided, when the scene’s physics are simulated, the physics body updates the node’s position and rotates the node.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Converts a point from the coordinate system of another node in the node tree to the coordinate system of this node.

    Declaration

    Swift

    func convertPoint(_ point: CGPoint, fromNode node: SKNode) -> CGPoint

    Objective-C

    - (CGPoint)convertPoint:(CGPoint)point fromNode:(SKNode *)node

    Parameters

    point

    A point in the other node’s coordinate system.

    node

    Another node in the same node tree as this node.

    Return Value

    The same point converted to this node’s coordinate system.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Converts a point in this node’s coordinate system to the coordinate system of another node in the node tree.

    Declaration

    Swift

    func convertPoint(_ point: CGPoint, toNode node: SKNode) -> CGPoint

    Objective-C

    - (CGPoint)convertPoint:(CGPoint)point toNode:(SKNode *)node

    Parameters

    point

    A point in this node’s coordinate system.

    node

    Another node in the same node tree as this node.

    Return Value

    The same point converted to the other node’s coordinate system.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Returns a Boolean value that indicates whether a point lies inside the node’s bounding box.

    Declaration

    Swift

    func containsPoint(_ p: CGPoint) -> Bool

    Objective-C

    - (BOOL)containsPoint:(CGPoint)p

    Parameters

    p

    A point in the node’s coordinate system.

    Return Value

    YEStrue if the point lies inside the node’s bounding box; otherwise NOfalse.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Returns the deepest descendant that intersects a point.

    Declaration

    Swift

    func nodeAtPoint(_ p: CGPoint) -> SKNode

    Objective-C

    - (SKNode *)nodeAtPoint:(CGPoint)p

    Parameters

    p

    A point in the node’s coordinate system.

    Return Value

    A descendant in the subtree that intersects the point, or the receiver if no nodes intersect the point. If multiple descendants intersect the point, the deepest node in the tree is returned. If multiple nodes are at the same level, the intersecting node with the largest z position is returned.

    Discussion

    A point is considered to be in a node if it lies inside the rectangle returned by the calculateAccumulatedFrame method.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Returns an array of all descendants that intersect a point.

    Declaration

    Swift

    func nodesAtPoint(_ p: CGPoint) -> [AnyObject]

    Objective-C

    - (NSArray *)nodesAtPoint:(CGPoint)p

    Parameters

    p

    A point in the node’s coordinate system.

    Return Value

    An array of all SKNode objects in the subtree that intersect the point. If no nodes intersect the point, an empty array is returned.

    Discussion

    A point is considered to be in a node if it lies inside the rectangle returned by the calculateAccumulatedFrame method.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Returns a Boolean value that indicates whether this node intersects the specified node.

    Declaration

    Swift

    func intersectsNode(_ node: SKNode) -> Bool

    Objective-C

    - (BOOL)intersectsNode:(SKNode *)node

    Parameters

    node

    Another node in the same node tree.

    Return Value

    YEStrue if the two nodes intersect; otherwise NOfalse.

    Discussion

    The two nodes are considered to intersect if their frames intersect. The children of both nodes are ignored in this test.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • userData userData Property

    A dictionary containing arbitrary data.

    Declaration

    Swift

    var userData: NSMutableDictionary?

    Objective-C

    @property(nonatomic, retain) NSMutableDictionary *userData

    Discussion

    You use this property to store your own data in a node. For example, you might store game-specific data about each node to use inside your game logic. This can be a useful alternative to creating your own node subclasses to hold game data.

    Sprite Kit does not do anything with the data stored in the node. However, the data is archived when the node is archived.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Specifies the list of constraints to apply to the node.

    Declaration

    Swift

    var constraints: [AnyObject]?

    Objective-C

    @property(nonatomic, copy) NSArray *constraints

    Discussion

    Assign an array of SKConstraint objects to the node. The scene processes these constraints before the scene is rendered. The constraints are processed in array order. If multiple nodes in the node tree have constraints, there is no guaranteed order that the nodes are processed in.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later

  • Specifies the reach constraints to apply to the node when executing a reach action.

    Declaration

    Swift

    @NSCopying var reachConstraints: SKReachConstraints?

    Objective-C

    @property(nonatomic, copy) SKReachConstraints *reachConstraints

    Discussion

    To use inverse kinematics, create a new SKReachConstraints object and assign it to this property. When a reach action calculates the new positions of this node, the possible values for this node are restricted to the constraints defined by this object. For more information on the inverse kinematic actions, see SKAction Class Reference.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later

  • The modes that describe how the source and destination pixel colors are used to calculate the new destination color.

    Declaration

    Swift

    enum SKBlendMode : Int { case Alpha case Add case Subtract case Multiply case MultiplyX2 case Screen case Replace }

    Objective-C

    typedef SKBlendMode : NSInteger { SKBlendModeAlpha = 0, SKBlendModeAdd = 1, SKBlendModeSubtract = 2, SKBlendModeMultiply = 3, SKBlendModeMultiplyX2 = 4, SKBlendModeScreen = 5, SKBlendModeReplace = 6, } SKBlendMode;

    Constants

    • Alpha

      SKBlendModeAlpha

      The source and destination colors are blended by multiplying the source alpha value.

      Available in iOS 7.0 and later

    • Add

      SKBlendModeAdd

      The source and destination colors are added together.

      Available in iOS 7.0 and later

    • Subtract

      SKBlendModeSubtract

      The source color is subtracted from the destination color.

      Available in iOS 7.0 and later

    • Multiply

      SKBlendModeMultiply

      The source color is multiplied by the destination color.

      Available in iOS 7.0 and later

    • MultiplyX2

      SKBlendModeMultiplyX2

      The source color is multiplied by the destination color and then doubled.

      Available in iOS 7.0 and later

    • Screen

      SKBlendModeScreen

      The source color is added to the destination color times the inverted source color.

      Available in iOS 7.0 and later

    • Replace

      SKBlendModeReplace

      The source color replaces the destination color.

      Available in iOS 7.0 and later

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later