Class

SKNode

The base class of all SpriteKit nodes.

Declaration

iOS, tvOS
@interface SKNode : UIResponder
macOS
@interface SKNode : NSResponder
watchOS
@interface SKNode : NSObject

Overview

SKNode provides base properties for its subclasses and it can be used as a container or layout tool for other nodes. For example, you might add a collection of nodes as children to an SKNode that all move together within the scene; because nodes inherit the properties of their parent, changing the parent node's position propagates the change to its children as well.

SKNode does not draw any content itself. Its visual counterparts are listed in Nodes that Draw.

Topics

First Steps

Getting Started with Nodes

Learn about the fundamental properties that provide a foundation for all other nodes.

- init

Initializes a blank node.

+ node

Creates a new node.

+ nodeWithFileNamed:

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

- initWithCoder:

Called when a node is initialized from an .sks file.

Positioning Content in a Scene

Lay out content in a scene by positioning it within its parent's coordinate system.

position

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

Querying the Content Size

Read the location and size of a node or its children in the parent's coordinate space.

frame

A rectangle in the parent’s coordinate system that contains the node’s content, ignoring the node’s children.

- calculateAccumulatedFrame

Returns a rectangle in the parent’s coordinate system that contains the position and size of itself and all child nodes.

Configuring Draw Order

About Node Drawing Order

Understand how SpriteKit layers your scene's nodes from top to bottom.

zPosition

The height of the node relative to its parent.

Scaling and Rotating

zRotation

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

- setScale:

Sets the xScale and yScale properties of the node.

xScale

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

yScale

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

Accessing Related Nodes

About SpriteKit Coordinate Systems

Learn how a node conforms to its coordinate systems.

scene

The scene node that contains this node.

parent

The node’s parent node.

children

The node’s children.

Modifying the Node Tree

Accessing and Modifying the Node Tree

See the objects and functions you use to control the node tree's composition.

- addChild:

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

- insertChild:atIndex:

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

- isEqualToNode:

Compares the parameter node to the receiving node.

- moveToParent:

Moves the node to a new parent node in the scene.

- removeFromParent

Removes the receiving node from its parent.

- removeAllChildren

Removes all of the node’s children.

- removeChildrenInArray:

Removes a list of children from the receiving node.

- inParentHierarchy:

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

Customizing Nodes

Customizing the Behavior of a Node

Organize your app's logic and display code with nodes.

Propagating Properties to Children

About Node Property Propagation

Learn which properties of a node affect its child nodes.

Accessing Nodes by Name

Access nodes by name instead of instance properties.

Searching the Node Tree

Access nodes by name to avoid needing an instance variable.

name

The node’s assignable name.

- childNodeWithName:

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

- enumerateChildNodesWithName:usingBlock:

Searches the children of the receiving node to perform processing for nodes that share a name.

- objectForKeyedSubscript:

Returns an array of nodes that match the name parameter.

Altering Node Visibility

Control whether a node is visible or semitransparent.

alpha

The transparency value applied to the node’s contents.

hidden

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

Running Actions

Run actions on a node and control their timing.

Getting Started with Actions

Create, configure, and run actions in SpriteKit.

- runAction:

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

- runAction:completion:

Adds an action to the list of actions executed by the node and schedules the argument block to be run upon completion of the action.

- runAction:withKey:

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

speed

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

paused

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

- actionForKey:

Returns an action associated with a specific key.

- hasActions

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

- removeAllActions

Ends and removes all actions from the node.

- removeActionForKey:

Removes an action associated with a specific key.

Adding Physics Behaviors

Enable physics behaviors by adding a body.

Getting Started with Physics Bodies

Create and assign a physics body to enable physics.

physicsBody

The physics body associated with the node.

Constraining Node Position or Rotation

Define positional contraints relating to other nodes in the scene.

constraints

A list of constraints to apply to the node.

reachConstraints

The reach constraints to apply to the node when executing a reach action.

Detecting Collisions Manually

As an alternative to SKPhysicsContactDelegate, manually check if two nodes overlap.

- intersectsNode:

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

Adding GameplayKit Behaviors

Enable GameplayKit behaviors by defining an entity and its obstacles in a scene.

entity

The GameplayKit entity this node represents.

+ obstaclesFromNodeBounds:

Converts each node into an obstacle by transforming its bounds into the scene’s coordinate system.

+ obstaclesFromNodePhysicsBodies:

Converts each node into an obstacle by transforming the node’s physics body shape into the scene’s coordinate system.

+ obstaclesFromSpriteTextures:accuracy:

Turns each node into an obstacle by changing the node’s texture into a physics shape and converting it into the scene’s coordinate system.

Handling User Input

Enable user interaction to allow a node to respond to user input.

Controlling User Interaction on Nodes

Enable your node to respond to user input, like touches or mouse clicks.

userInteractionEnabled

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

focusBehavior

The focus behavior for a node.

Hit Testing

Understanding Hit-Testing

Learn how find child nodes at a given point by using hit-testing.

- containsPoint:

Returns a Boolean value that indicates whether a point lies inside the parent’s coordinate system.

- nodeAtPoint:

Returns the deepest visible descendant that intersects a point.

- nodesAtPoint:

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

Converting Between Coordinate Systems of Different Nodes

Converting Coordinate Spaces

Convert positions across the various coordinate spaces in a scene.

- convertPoint:fromNode:

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

- convertPoint:toNode:

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

Adding Custom Data Without Subclassing

Quickly add data to a node without having to subclass it.

userData

A dictionary containing arbitrary data.

Providing Accessibility

Extend your app's usability by exposing certain nodes in your scene as Accessibility user interface elements.

accessibilityChildren

An array of user interface elements that represent children of this element.

accessibilityFrame

The size of this user interface element, in screen points.

accessibilityHelp

The help description of this user interface element; for example, the text shown in a tooltip.

accessibilityLabel

A short description of this user interface element.

accessibilityParent

The user interface element that contains this element.

accessibilityRole

A string value describing the user interface element type; for example, a button.

accessibilityRoleDescription

A string value describing the user interface element name and type; for example, the Buy button.

accessibilitySubrole

A string that defines this user interface element's subrole; for example, a full-screen button.

accessibilityElement

A toggle you implement to indicate to the system whether this user interface element should be exposed to the user.

accessibilityEnabled

A toggle you implement to indicate to the system whether this user interface element should respond to user input.

- accessibilityHitTest:

Returns the frontmost user interface element in the element hierarchy.

Setting a Node's Unique Attributes for a Shader

Set the values that make a node unique to a shader.

attributeValues

The values of each attribute associated with the node's attached shader.

Deprecated
- setValue:forAttributeNamed:

Sets an attribute value for an attached shader

Deprecated
- valueForAttributeNamed:

The value of a shader attribute.

Deprecated

Relationships

See Also

Base Nodes

Using Base Nodes to Lay Out SpriteKit Content

Use nonvisual nodes to define the layout of a scene.

SKCameraNode

A node that determines which parts of the scene are visible within a view.

SKReferenceNode

A node that's defined in an archived .sks file.