Class

SKScene

The root node for all Sprite Kit objects displayed in a view.

Overview

An SKScene object represents a scene of content in Sprite Kit. A scene is the root node in a tree of Sprite Kit nodes (SKNode). These nodes provide content that the scene animates and renders for display. To display a scene, you present it from an SKView object.

A scene calculates the contents of a new frame by processing the following actions in order:

  1. The scene calls its update(_:) method.

  2. The scene executes actions on its children.

  3. The scene calls its didEvaluateActions() method.

  4. The scene executes any physics simulations on physics bodies in the scene.

  5. The scene calls its didSimulatePhysics() method.

  6. The scene applies any constraints associated with nodes in the scene.

  7. The scene calls its didApplyConstraints() method.

  8. The scene calls its didFinishUpdate() method.

  9. The scene renders all of its nodes and updates the view to display the new contents.

Your game can provide a delegate object to the scene. The delegate protocol specifies a set of methods that correspond to the methods listed above. If a delegate is provided and it implements one of these methods, the delegate’s method is called instead of the scene method.

Subclassing Notes

Often, your game subclasses a scene to deliver gameplay. Your subclass usually:

  • Creates the scene’s initial content

  • Implements game logic that occurs each time a frame is processed

  • Implements responder methods to handle keyboard, mouse, or touch events

An alternative pattern to subclassing the SKScene class is to use a delegate that implements most of the game logic. For example, in an iOS game, you can make your view controller the delegate for your scene. The view controller already participates in event handling and can perform all of the other necessary duties described above.

Symbols

Initializing a Scene

init(size: CGSize)

Initializes a new scene object.

Determining What Portion of the Scene Is Visible in the View

var camera: SKCameraNode?

The camera node in the scene that determines what part of the scene’s coordinate space is visible in the view.

var anchorPoint: CGPoint

The point in the view’s frame that corresponds to the scene’s origin.

var size: CGSize

The dimensions of the scene in points.

func didChangeSize(CGSize)

Called whenever the scene’s size changes.

var scaleMode: SKSceneScaleMode

Defines how the scene is mapped to the view that presents it.

Setting the Background Color of a Scene

var backgroundColor: UIColor

The background color of the scene.

Converting Between View and Scene Coordinates

func convertPoint(fromView: CGPoint)

Converts a point from view coordinates to scene coordinates.

func convertPoint(toView: CGPoint)

Converts a point from scene coordinates to view coordinates.

Presenting a Scene

func willMove(from: SKView)

Called immediately before a scene is removed from a view.

func didMove(to: SKView)

Called immediately after a scene is presented by a view.

var view: SKView?

The view that is currently presenting the scene.

Executing the Animation Loop

var delegate: SKSceneDelegate?

A delegate to be called during the animation loop.

func update(TimeInterval)

Performs any scene-specific updates that need to occur before scene actions are evaluated.

func didEvaluateActions()

Performs any scene-specific updates that need to occur after scene actions are evaluated.

func didSimulatePhysics()

Performs any scene-specific updates that need to occur after physics simulations are performed.

func didApplyConstraints()

Performs any scene-specific updates that need to occur after constraints are applied.

func didFinishUpdate()

Called after the scene has finished all of the steps required to process animations.

Working with Physics in the Scene

var physicsWorld: SKPhysicsWorld

The physics simulation associated with the scene.

Working with Audio in the Scene

var audioEngine: AVAudioEngine

The AV Foundation audio engine used to play audio from audio nodes contained in the scene.

var listener: SKNode?

A node used to determine the position of the listener for positional audio in the scene.

Constants

SKSceneScaleMode

The modes that determine how the scene’s area is mapped to the view that presents it.

Instance Methods

Relationships

Inherits From