Configure the View and Load the Scene

In the Project Navigator, select the GameViewController.swift file to open it in the Source Editor. Locate and review the viewDidLoad() method. It looks like this:

 override​ ​func​ ​viewDidLoad​() {
 super​.​viewDidLoad​()
 
 // Load 'GameScene.sks' as a GKScene. This provides gameplay-related content
 // including entities and graphs
 if​ ​let​ scene = ​GKScene​(fileNamed: ​"GameScene"​) {
 
 // Get the SKScene from the loaded GKScene
 if​ ​let​ sceneNode = scene.rootNode ​as!​ ​GameScene​? {
 
 // Copy gameplay-related content over to the scene
  sceneNode.entities = scene.entities
  sceneNode.graphs = scene.graphs
 
 // Set the scale mode to scale to fit the window
  sceneNode.scaleMode = .aspectFill
 
 // Present the scene
 if​ ​let​ view = ​self​.view ​as!​ ​SKView​? {
  view.​presentScene​(sceneNode)
 
  view.ignoresSiblingOrder = ​true
 
  view.showsFPS = ​true
  view.showsNodeCount = ​true
  }
  }
  }
 }

Some of this code in this method may look a little familiar, such as the line that reads view.ignoresSiblingOrder = true. This code is what dictates, in part, how the scene renders. Change that line to read:

 view.ignoresSiblingOrder = ​false

By setting this property to false, each node’s render order within the scene won’t be arbitrary anymore; instead, the scene will render its nodes in the order in which they appear in the node tree. In this case:

  • background

  • player

  • controller, along with its five children: controller_stop, controller_left, controller_right, controller_up, and controller_down

  • button_attack

While you were updating the viewDidLoad() method, did you happen to notice this code:

 if​ ​let​ scene = ​GKScene​(fileNamed: ​"GameScene"​)

which is sort of equivalent to this (something you saw earlier in the book):

 if​ ​let​ scene = ​SKScene​(fileNamed: ​"GameScene"​)

The only difference is that the first example uses a GKScene object rather than an SKScene object, which is what you used for Gloop Drop.

If you recall, in Create Your First SpriteKit Scene, you were creating the GameScene like this:

 // 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,
  green: 157/255,
  blue: 181/255,
  alpha: 1.0)
 
 // Present the scene
  view.​presentScene​(scene)
 
 // Set the view options
  view.ignoresSiblingOrder = ​false
  view.showsPhysics = ​false
  view.showsFPS = ​true
  view.showsNodeCount = ​true
 }

For Val’s Revenge, you’ll be using more GameplayKit features, so it makes sense to use the GKScene object instead. You’ll learn more about GameplayKit and the GKScene class in Part 3, Scale Your Games with GameplayKit.

There’s one more thing: the fileNamed parameter is stuffed with the name (minus the extension, sks) of the scene you just built:

 if​ ​let​ scene = ​GKScene​(fileNamed: ​"GameScene"​)

As you might have guessed, this parameter is how you load the scene using a scene file.

Build and run the project—multiple times—and you’ll see all of your nodes, every time.

images/BuildingScenesWithTheSceneEditor/scene-editor-huzzah.png
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset