Write Code to Interface with the Scene

In Configure the View and Load the Scene, you learned how to load a scene file through code. If you recall, loading a scene file takes place in the viewDidLoad() method of the GameViewController.swift file and looks like this:

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

But loading the scene is only half of the story—you still need to write the code that interfaces with the objects within the scene, for example, the player node. In this section, you’ll concentrate on making that connection.

To begin, open the valsrevenge project in Xcode.

Creating the Player Class

The goal here is to hook up the player node and the controller nodes and get the player moving around using the D-pad. Rather than place this code into the GameScene.swift file, you’ll create a separate file to hold the Player class. If you need a refresher on how to create files in Xcode, refer to Create the Player Class, otherwise, create a new Swift file, name it Player.swift, and add the following code to set up the class and the two main method stubs:

 import​ ​SpriteKit
 
 enum​ ​Direction​: ​String​ {
 case​ stop
 case​ left
 case​ right
 case​ up
 case​ down
 }
 
 class​ ​Player​: ​SKSpriteNode​ {
 
 func​ ​move​(_ direction: ​Direction​) {
 print​(​"move player: ​​(​direction.rawValue​)​​"​)
  }
 
 func​ ​stop​() {
 print​(​"stop player"​)
  }
 }

This code sets up an enum that holds the different directions the player can move. It also sets up the Player class and two stub methods: one to move the player and one to stop the player.

Now that you’ve got the Player class set up, you need a way to connect this class to the player sprite node you added to the scene in Adding Other Nodes.

Using a Custom Class in the Scene Editor

Return to the GameScene.sks file. Select the player node and switch to the Custom Class Inspector—it’s the second inspector from the right.

Notice how the Custom Class defaults to the standard SKSpriteNode class. Instead of using that class, you’ll use the new custom Player class you just created. To make the switch, enter Player into the Custom Class field, like so:

images/UsingTheSceneEditorToAddPhysics/scene-edit-phy-set-custom-class.png

With the player node now using the custom Player class, you’re ready to write the code that connects the nodes in your scene to the properties and objects you create in your code.

Finding the Player Node from the Game Scene

In Use Attributed Strings with Labels, you saw how you can use the childNode(withName:) method to check for a node with a matching name. Once again, you’ll use that method to locate the player node.

In the Project Navigator, select the GameScene.swift file to open it in the Source Editor.

First, add a new property for the player node:

 private​ ​var​ player: ​Player​?

Next, add an override for the didMove(to:) method, and inside that method, place the following code to initialize the new property (add it below the sceneDidLoad() method):

 override​ ​func​ ​didMove​(to view: ​SKView​) {
  player = ​childNode​(withName: ​"player"​) ​as?​ ​Player
  player?.​move​(.stop)
 }

If you recall, the last time you used the childNode(withName:) method, you did so using an advanced search. This time, however, you do a simple search that looks for any node that has the name player—and exists as a direct child of (in this case) the Scene node. Alternatively, you can use the class name instead, like this:

 player = ​childNode​(withName: ​"Player"​) ​as?​ ​Player

Either way, the search finds and returns the player node you added in the GameScene.sks file. For more information about the different types of search options, refer to Searching the Node Tree[37] in Apple’s documentation.

Build and run the project. Look at the console and you’ll see the “move player: stop” statement:

images/UsingTheSceneEditorToAddPhysics/scene-edit-phy-move-player-console.png

Excellent. Your player sprite is recognized in your code, and you’re ready to implement the controller movement. For that, you’ll use the physics engine.

..................Content has been hidden....................

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