iOS Developer Library

Developer

SpriteKit Framework Reference SKScene Class Reference

Options
Deployment Target:

On This Page
Language:

SKScene

Conforms To


Import Statement


Swift

import SpriteKit

Objective-C

@import SpriteKit;

Availability


Available in iOS 7.0 and later.

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.

  • Creates and returns a new scene object.

    Declaration

    Objective-C

    + (instancetype)sceneWithSize:(CGSize)size

    Parameters

    size

    The size of the scene in points.

    Return Value

    A new scene object.

    Discussion

    This class method allocates a new scene object and calls its initWithSize: initializer method.

    Import Statement

    Objective-C

    @import SpriteKit;

    Availability

    Available in iOS 7.0 and later.

  • Initializes a new scene object.

    Declaration

    Swift

    init(size size: CGSize)

    Objective-C

    - (instancetype)initWithSize:(CGSize)size

    Parameters

    size

    The size of the scene in points.

    Return Value

    A newly initialized scene object.

    Discussion

    This is the class’s designated initializer method.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    var anchorPoint: CGPoint

    Objective-C

    @property(nonatomic) CGPoint anchorPoint

    Discussion

    When a scene is presented, the size and anchorPoint properties determine the portion of the scene’s coordinate space that is visible in the view.

    You specify the value using the unit coordinate space. The default value is (0,0), which corresponds to the lower-left corner of the view’s frame rectangle.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • size size Property

    The dimensions of the scene in points.

    Declaration

    Swift

    var size: CGSize

    Objective-C

    @property(nonatomic) CGSize size

    Discussion

    When a scene is presented, the size and anchorPoint properties determine the portion of the scene’s coordinate space that is visible in the view.

    If you set the size property to a new value, the scene’s didChangeSize: method is called. This property can also change if the scaleMode property is set to SKSceneScaleModeResizeFill and the presenting view is resized. After the scene’s size changes, future updates are rendered immediately at the new size.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Called whenever the scene’s size changes.

    Declaration

    Swift

    func didChangeSize(_ oldSize: CGSize)

    Objective-C

    - (void)didChangeSize:(CGSize)oldSize

    Parameters

    oldSize

    The old size of the scene, in points.

    Discussion

    This method is intended to be overridden in a subclass. Typically, you use this method to adjust the positions of nodes in the scene.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

    See Also

    size

  • scaleMode scaleMode Property

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

    Declaration

    Swift

    var scaleMode: SKSceneScaleMode

    Objective-C

    @property(nonatomic) SKSceneScaleMode scaleMode

    Discussion

    It is possible for a scene’s size to differ from the size of the view it is presented in. The scale mode determines how the scene is mapped to the view. The possible values are listed in “Scene Scaling Modes”. The default value is SKSceneScaleModeFill.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • The background color of the scene.

    Declaration

    Swift

    var backgroundColor: UIColor

    Objective-C

    @property(nonatomic, retain) UIColor *backgroundColor

    Discussion

    The default value is a gray color (RGBA 0.15, 0.15, 0.15, 1.0).

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Converts a point from view coordinates to scene coordinates.

    Declaration

    Swift

    func convertPointFromView(_ point: CGPoint) -> CGPoint

    Objective-C

    - (CGPoint)convertPointFromView:(CGPoint)point

    Parameters

    point

    A point in view coordinates.

    Return Value

    The same point in the scene’s coordinate system.

    Discussion

    The scene must be presented in a view before calling this method.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Converts a point from scene coordinates to view coordinates.

    Declaration

    Swift

    func convertPointToView(_ point: CGPoint) -> CGPoint

    Objective-C

    - (CGPoint)convertPointToView:(CGPoint)point

    Parameters

    point

    A point in scene coordinates.

    Return Value

    The same point in the view’s coordinate system.

    Discussion

    The scene must be presented in a view before calling this method.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Called immediately before a scene is removed from a view.

    Declaration

    Swift

    func willMoveFromView(_ view: SKView)

    Objective-C

    - (void)willMoveFromView:(SKView *)view

    Parameters

    view

    The view that is presenting the scene.

    Discussion

    This method is intended to be overridden in a subclass. You can use this method to implement any custom behavior for your scene when it is about to be removed from the view.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • Called immediately after a scene is presented by a view.

    Declaration

    Swift

    func didMoveToView(_ view: SKView)

    Objective-C

    - (void)didMoveToView:(SKView *)view

    Parameters

    view

    The view that is presenting the scene.

    Discussion

    This method is intended to be overridden in a subclass. You can use this method to implement any custom behavior for your scene when it is about to be presented by a view. For example, you might use this method to create the scene’s contents.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • view view Property

    The view that is currently presenting the scene. (read-only)

    Declaration

    Swift

    weak var view: SKView? { get }

    Objective-C

    @property(nonatomic, weak, readonly) SKView *view

    Discussion

    To present a scene, you call the presentScene: method or presentScene:transition: method on the SKView class. If the scene is not currently presented, this property holds nil.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

  • delegate delegate Property

    A delegate to be called during the animation loop.

    Declaration

    Swift

    unowned(unsafe) var delegate: SKSceneDelegate?

    Objective-C

    @property(nonatomic, assign) id< SKSceneDelegate > delegate

    Discussion

    When a delegate is present, when any of the animation loop methods steps are executed, your delegate is called. Typically, you use a delegate when you do not want to implement a scene subclass or if you want to dynamically change the scene behavior at runtime.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

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

    Declaration

    Swift

    func update(_ currentTime: NSTimeInterval)

    Objective-C

    - (void)update:(NSTimeInterval)currentTime

    Parameters

    currentTime

    The current system time.

    Discussion

    Do not call this method directly; it is called exactly once per frame, so long as the scene is presented in a view and is not paused. By default, this method does nothing. Your scene subclass should override this method and perform any necessary updates to the scene.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    func didEvaluateActions()

    Objective-C

    - (void)didEvaluateActions

    Discussion

    Do not call this method directly; it is called exactly once per frame, so long as the scene is presented in a view and is not paused. By default, this method does nothing. Your scene subclass should override this method and perform any necessary updates to the scene.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    func didSimulatePhysics()

    Objective-C

    - (void)didSimulatePhysics

    Discussion

    Do not call this method directly; it is called exactly once per frame, so long as the scene is presented in a view and is not paused. By default, this method does nothing. Your scene subclass should override this method and perform any necessary updates to the scene.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    func didApplyConstraints()

    Objective-C

    - (void)didApplyConstraints

    Discussion

    Do not call this method directly; it is called exactly once per frame, so long as the scene is presented in a view and is not paused. By default, this method does nothing. Your scene subclass should override this method and perform any necessary updates to the scene.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

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

    Declaration

    Swift

    func didFinishUpdate()

    Objective-C

    - (void)didFinishUpdate

    Discussion

    Do not call this method directly; it is called exactly once per frame, so long as the scene is presented in a view and is not paused. By default, this method does nothing. Your scene subclass should override this method and perform any necessary updates to the scene. This method is the last method to be called before the scene is rendered.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 8.0 and later.

  • The physics simulation associated with the scene. (read-only)

    Declaration

    Swift

    var physicsWorld: SKPhysicsWorld { get }

    Objective-C

    @property(nonatomic, readonly) SKPhysicsWorld *physicsWorld

    Discussion

    Every scene automatically creates a physics world object to simulate physics on nodes in the scene. You use this property to access the scene’s global physics properties, such as gravity. To add physics to a particular node, see physicsBody.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    enum SKSceneScaleMode : Int { case Fill case AspectFill case AspectFit case ResizeFill }

    Objective-C

    typedef NS_ENUM (NSInteger, SKSceneScaleMode ) { SKSceneScaleModeFill, SKSceneScaleModeAspectFill, SKSceneScaleModeAspectFit, SKSceneScaleModeResizeFill }

    Constants

    • Fill

      SKSceneScaleModeFill

      Each axis of the scene is scaled independently so that each axis in the scene exactly maps to the length of that axis in the view.

      Available in iOS 7.0 and later.

    • AspectFill

      SKSceneScaleModeAspectFill

      The scaling factor of each dimension is calculated and the larger of the two is chosen. Each axis of the scene is scaled by the same scaling factor. This guarantees that the entire area of the view is filled but may cause parts of the scene to be cropped.

      Available in iOS 7.0 and later.

    • AspectFit

      SKSceneScaleModeAspectFit

      The scaling factor of each dimension is calculated and the smaller of the two is chosen. Each axis of the scene is scaled by the same scaling factor. This guarantees that the entire scene is visible but may require letterboxing in the view.

      Available in iOS 7.0 and later.

    • ResizeFill

      SKSceneScaleModeResizeFill

      The scene is not scaled to match the view. Instead, the scene is automatically resized so that its dimensions always match those of the view.

      Available in iOS 7.0 and later.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later.