Clean Up the Default Template

As with most default project templates, the default iOS Game template includes a lot of boilerplate code and files you won’t need, so it’s best to remove these things.

In the Project Navigator, select the GameScene.sks and Actions.sks files. You’ll learn more about SKS files in Chapter 7, Building Scenes with the Scene Editor, but for now, right-click these two files, select Delete, and then Move to Trash, as shown in the image.

images/CreatingScenesWithSpritesAndNodes/spritekit-remove-files.png

Still inside the Project Navigator, select the GameScene.swift file, which opens the file in the Source Editor on the right. This file controls the main game scene, and it comes packed with all sorts of properties and methods. As you work through this book, you’ll learn more about this file, but for now, delete everything in that file and replace it with this:

 import​ ​SpriteKit
 import​ ​GameplayKit
 
 class​ ​GameScene​: ​SKScene​ {
 
 override​ ​func​ ​didMove​(to view: ​SKView​) {
 
  }
 }

This code imports the SpriteKit and GameplayKit frameworks and declares a GameScene class with a single, empty method named didMove(to:). This method gets called automatically when the view is about to present the scene.

Next, open the GameViewController.swift file. This is the file that controls the main view controller of your game. The view controller is responsible for loading the view.

Find and remove all of the code inside the viewDidLoad() method, leaving only the line that reads super.viewDidLoad(). Leaving this line intact ensures that the superclass loads everything it needs to function before you start overriding its method.

When you’re done, the GameViewController.swift file will look like this:

 import​ ​UIKit
 import​ ​SpriteKit
 import​ ​GameplayKit
 
 class​ ​GameViewController​: ​UIViewController​ {
 
 override​ ​func​ ​viewDidLoad​() {
 super​.​viewDidLoad​()
  }
 
 override​ ​var​ shouldAutorotate: ​Bool​ {
 return​ ​true
  }
 
 override​ ​var​ supportedInterfaceOrientations: ​UIInterfaceOrientationMask​ {
 if​ ​UIDevice​.current.userInterfaceIdiom == .phone {
 return​ .allButUpsideDown
  } ​else​ {
 return​ .all
  }
  }
 
 override​ ​var​ prefersStatusBarHidden: ​Bool​ {
 return​ ​true
  }
 }

You now have a nice clean view controller, ready and waiting. It’s time to start building your game.

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

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