- Game Development with Swift
- Stephen Haney
- 468字
- 2021-07-16 13:45:40
Sharpening our pencils
There are four quick items to take care of before we start drawing:
- Since we will design our game to use landscape screen orientations, we will disable the portrait view altogether:
- With your game project open in Xcode, select the overall project folder in the project navigator (the top-most item).
- You will see your project settings in the main frame of Xcode. Under Deployment Info, find the Device Orientation section.
- Uncheck the Portrait option, as shown in the following screenshot:
- The SpriteKit template generates a visual layout file for arranging sprites in our scene. We will not need it; we will use the SpriteKit visual editor later when we explore level design. To delete this extra file:
- Right-click on
GameScene.sks
in the project navigator and choose delete. - Choose Move to Trash in the dialog window.
- Right-click on
- We need to resize our scene to fit the new landscape view. Follow these steps to resize the scene:
- Open
GameViewController.swift
from the project navigator and locate theviewDidLoad
function inside theGameViewController
class. TheviewDidLoad
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. DeleteviewDidLoad
completely, removing all of its code. - Replace
viewDidLoad
with a new function namedviewWillLayoutSubviews
. Do not worry about understanding every line right now; we are just configuring our project. Use this code forviewWillLayoutSubviews
:override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() // Create our scene: let scene = GameScene() // Configure the view: let skView = self.view as! SKView skView.showsFPS = true skView.showsNodeCount = true skView.ignoresSiblingOrder = true scene.scaleMode = .AspectFill // size our scene to fit the view exactly: scene.size = view.bounds.size // Show the new scene: skView.presentScene(scene) }
- Lastly, in
GameViewController.swift
, find thesupportedInterfaceOrientations
function and reduce it to this code:override func supportedInterfaceOrientations() -> Int { return Int( UIInterfaceOrientationMask.Landscape.rawValue); }
Tip
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Additionally, each chapter provides checkpoint links you can use to download the example project to that point.
- Open
- We should double-check that we are ready to move on. Try to run our 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 in 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, to finish your project preparation.
推薦閱讀
- PyTorch深度學習實戰(zhàn):從新手小白到數(shù)據(jù)科學家
- 數(shù)據(jù)分析實戰(zhàn):基于EXCEL和SPSS系列工具的實踐
- 數(shù)據(jù)可視化:從小白到數(shù)據(jù)工程師的成長之路
- 深度剖析Hadoop HDFS
- 智能數(shù)據(jù)時代:企業(yè)大數(shù)據(jù)戰(zhàn)略與實戰(zhàn)
- Proxmox VE超融合集群實踐真?zhèn)?/a>
- 大數(shù)據(jù)精準挖掘
- INSTANT Apple iBooks How-to
- Hadoop集群與安全
- 聯(lián)動Oracle:設(shè)計思想、架構(gòu)實現(xiàn)與AWR報告
- 活用數(shù)據(jù):驅(qū)動業(yè)務(wù)的數(shù)據(jù)分析實戰(zhàn)
- 企業(yè)大數(shù)據(jù)處理:Spark、Druid、Flume與Kafka應(yīng)用實踐
- 改進的群智能算法及其應(yīng)用
- AndEngine for Android Game Development Cookbook
- 數(shù)字化轉(zhuǎn)型實踐:構(gòu)建云原生大數(shù)據(jù)平臺