Book Image

Swift by Example

By : Giordano Scalzo
Book Image

Swift by Example

By: Giordano Scalzo

Overview of this book

Table of Contents (15 chapters)

Adding a few touches

Although a few things are still missing, either some parts of them are straightforward or we have already implemented them in the previous chapters when we were building Flappy Swift.

The score

The score falls under the straightforward category, and it is worthwhile implementing it right now, so we can finish adding all the visual elements to the screen.

The goal of the game is for the player to keep going as long as they can without colliding with a cube. So to implement the score, we just need to schedule a timer that fires every second, increasing the score. First of all, we need to add the elements as properties:

class GameViewController: UIViewController {
    private var laneTimer: NSTimer!
    private let scoreLbl = UILabel()
    private var scoreTimer: NSTimer!
    private var score = 0

Then we set up the score, calling setupScore() in createContents():

    func createContents() {
        scene!.fogColor = UIColor.blackColor()