Create Your First SpriteKit Scene

Apple Game Frameworks and Technologies — by Tammy Coron (24 / 193)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Set the Supported Device Orientation | TOC | Create Your First Sprite Node 👉

Before building your first scene, let’s go over the key components that make up a SpriteKit scene:

  • SKView: This is the primary view for a SpriteKit scene. The SKView class inherits from the UIView class (or NSView on macOS).
  • SKScene: This is the root node of the scene. The SKScene class includes properties and methods that define how to render content and process animation.

Typically, the view is responsible for presenting the scene.

You can create a scene in two ways: visually using the Scene Editor or programmatically in code using the Source Editor. For this first game, you’ll use code and the Source Editor to get better acquainted with what’s going on under the hood of SpriteKit.

Open the GameViewController.swift file and in the viewDidLoad() method, add the following code below the line that reads super.viewDidLoad():

​ ​// Create the view​
​ ​if​ ​let​ view = ​self​.view ​as!​ ​SKView​? {

​ ​// Create the scene​
​ ​let​ scene = ​GameScene​(size: view.bounds.size)

​ ​// Set the scale mode to scale to fill the view window​
​ scene.scaleMode = .aspectFill

​ ​// Set the background color​
​ scene.backgroundColor = ​UIColor​(red: 105/255,
​…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.