Game Development Reference
In-Depth Information
Following the SKLabelNode setup is the NSNumberFomatter setup section. The NSNum-
berForatter allows you to use numbers to be represented as string text. You will have a
maximum and minimum of one fraction digit. This will allow you to show the timer with-
in a tenth of a second. Don't forget you will also need to override the required initializer,
as in Listing 15-2 .
Listing 15-2 . Required Initializer
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
When your game starts, you will need start the timer so the user knows how fast he gets to
the enemy. To do this, you will create a function that will start the timer, and it will update
your SKLabelNode for the player. Listing 15-3 shows the startTimer() for this purpose.
Listing 15-3 . GameOverlay.startTimer( )
func startTimer() {
var startTime = NSDate.timeIntervalSinceReferenceDate()
var timerNode = self.childNodeWithName("timer") as
SKLabelNode
var timerAction = SKAction.runBlock({ () -> Void in
var now = NSDate.timeIntervalSinceReferenceDate()
var elapsedTime = NSTimeInterval( now - startTime )
var tempString = String(format: "%@",
self.timerFormat.stringFromNumber(elapsedTime)!)
timerNode.text = "Time: " + tempString
})
var startDelay = SKAction.waitForDuration(0.5)
var timerDelay = SKAction.sequence([timerAction,
startDelay])
var timer = SKAction.repeatActionForever(timerDelay)
self.timeNode.runAction(timer, withKey: "timerAction")
}
The next function you will need to add to the GameOverlay class is to stop the timer when
you have found the enemy. If you look at Listing 15-4 , you will see it is pretty simple.
Search WWH ::




Custom Search