官术网_书友最值得收藏!

Preparing your project

There are three quick items to take care of before we start drawing. To begin the preparation, go through the following steps:

  1. Since we will design our game to use landscape screen orientations, we will have to disable the portrait view altogether:
  2. With your game project open in Xcode, select the overall Project folder in the project navigator (the uppermost item).
  3. You will see your project settings in the main frame of Xcode. Under Deployment Info, find the Device Orientation section.
  4. Uncheck the Portrait option, as shown in the following screenshot:
    Preparing your project
  5. We need to resize our scene to fit the new landscape view. Follow these steps to resize the scene:
    1. Open GameViewController.swift from the project navigator and locate the viewDidLoad function inside the GameViewController class. The viewDidLoad function is going to fire before the game realizes it is in landscape view, so we need to use a function that fires later in the startup process. Delete viewDidLoad completely, removing all of its code.
    2. Replace viewDidLoad with a new function named viewWillLayoutSubviews. Do not worry about understanding every line right now; we are just configuring our project. Use this code for viewWillLayoutSubviews:
      Override func viewWillLayoutSubviews() { 
      
      super.viewWillLayoutSubviews()
      
      if let view = self.view as! SKView? { 
                          // Load the SKScene from 'GameScene.sks' 
      if let scene = SKScene(fileNamed: "GameScene") { 
                              // Set the scale mode fit the window: 
      scene.scaleMode = .aspectFill
                              // Size our scene to fit the view exactly: 
      scene.size = view.bounds.size
                              // Show the new scene: 
      view.presentScene(scene) 
                          } 
      
      view.ignoresSiblingOrder = true 
      view.showsFPS = true 
      view.showsNodeCount = true 
                      } 
                  } 
    3. Lastly, in GameViewController.swift, find the supportedInterfaceOrientations function and reduce it to this code:
      Override var supportedInterfaceOrientations:  
      UIInterfaceOrientationMask { 
      return .landscape 
                      } 

    Note

    Downloading the example code: You can download the example code files from your account at http://www.packtpub.com for all the Packt books you have purchased. If you have purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you. Additionally, each chapter provides checkpoint links that you can use to download the example project to that point.

  6. We should double-check that we are ready to move on. Try to run your clean project in the simulator using the toolbar play button or the command + R keyboard shortcut. After loading, the simulator should switch to landscape view with a blank gray background (and with the node and FPS counter at the bottom right). If the project will not run, or you still see "Hello, World", you will need to retrace your steps from the end of Chapter 1, Designing Games with Swift, namely, the section called Cleaning up, to finish your project preparation.

The following screenshot shows the output so far:

Preparing your project

Note

Checkpoint 2-A

I will provide checkpoints for each chapter. They are available in each chapter's code resources directory. I hope this will help you move forward if you are stuck or want to check your code against mine.

主站蜘蛛池模板: 北海市| 丽江市| 博罗县| 桂林市| 廉江市| 仙桃市| 海盐县| 静海县| 兰州市| 利川市| 滦平县| 迁西县| 广南县| 平定县| 黔西县| 岚皋县| 固镇县| 天祝| 德阳市| 体育| 孝感市| 大连市| 和政县| 洛宁县| 锦屏县| 闻喜县| 昌都县| 嘉荫县| 昌乐县| 恩平市| 赤壁市| 东莞市| 绥江县| 阜新| 葫芦岛市| 余姚市| 夹江县| 赤壁市| 陈巴尔虎旗| 宾阳县| 滨州市|