Although a few things are still missing, either some parts of them are straightforward or we already implemented them in the previous chapters when we were building Flappy Swift.
The score falls under the straightforward category, and it is worth implement it right now so that 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 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() setupScore() //....