iOS Developer Library

Developer

SpriteKit Framework Reference SKNode Class Reference

Options
Deployment Target:

On This Page
Language:

SKNode

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. More...

Conforms To


Import Statement


import SpriteKit @import SpriteKit;

Availability


Available in iOS 7.0 and later.
  • 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

    Availability

    Available in iOS 7.0 and later.

  • init() init Designated Initializer

    Initializes a new node.

    Declaration

    Swift

    init()

    Objective-C

    - (instancetype)init

    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

    import SpriteKit

    Availability

    Available in iOS 8.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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    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

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.