Monday, July 30, 2018

Introduction to iOS Game Development

My First App


1). Open Xcode and select File > New > Project... then choose Game and click Next. I fill in the details and choose options for the next page as following.


Xcode will create a sample application, which set the background of the screen to black and display "Hello World" in the middle of it. Every time I tap on the screen, a small square appears for a few second and then disappears. Below is the project files created by Xcode.


This sample application is small but it's still hard to catch up for the beginner like me so I modified it just to show only the black screen.

2). Delete GameScene.sks and Actions.sks files

3). Modify GameViewController.swift file as following:

import UIKit
import SpriteKit

class GameViewController: UIViewController {

    var scene: GameScene!
    
    override var prefersStatusBarHidden: Bool {
        return true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 1. Configure the main view
                   let skView = view as! SKView
                   skView.showsFPS = true
        
        // 2. Create and configure our game scene
                   scene = GameScene(size: skView.bounds.size)
        scene.scaleMode = .aspectFill
        
        // 3. Show the scene
        skView.presentScene(scene)
    }

}

4). Modify GameScene.swift file as following:

import SpriteKit

class GameScene: SKScene {
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(size: CGSize) {
        super.init(size: size)
        backgroundColor = SKColor(displayP3Red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
    }

}


Concepts 


The difference between the sample game application above and the normal Single View application is the view of the view controller. For a Single View application, the view controller's view is of type UIView. But for a 2D game application, the view must be of type SKView, which presents SpriteKit content or scenes (represented by SKScene objects). 

SKScene class is a descendant of SKNode class so we can think of a scene as a node. SKNode does not draw anything, but it applies its properties to its descendants. A scene builds a block for its contents and acts as a root node for a tree of node objects. The root node applies its properties to its descendants and the contents of its descendants. For example, if a node is rotated, all its descendants are also rotated. 

You may switch between scenes using a single SKView object in your window. You can use SKTransition to animate between two scenes.

In the example below, the scene has 5 nodes such as Sky, Missiles, Body, Rotor1, and Rotor2. Sky is the root node.

The image below shows how those nodes are rendered using zPosition and position properties.





References
1). Book: Beginning Swift Games Development for iOS
2). https://developer.apple.com/documentation/spritekit








No comments:

Post a Comment