0

I have created a timer function in swift SpriteKit and assigned it to a ScoreLabel. when the timer is active and updating itself, the undesired result is... 0:01 - 0:09 and then it is 0:010, I need to write some code to remove that second zero when the "second hand" changes from :09 to :10.

A little help would be great.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   for touch: AnyObject in touches {




    if ScoreLabel.text == "0"{

    let actionrun = SKAction.runBlock({
       self.score++
        self.timesecond++
        if self.timesecond == 60 {self.timesecond = 0}
        self.ScoreLabel.text = "\(self.score/60):0\(self.timesecond)"


          })

   ScoreLabel.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))

    }
Xcoder555
  • 77
  • 12

2 Answers2

0
self.ScoreLabel.text = String(format: "%d:%02d", self.score/60, self.timesecond)

Should do the trick!

%02d

Means that any number with less than two decimal digits will be zero padded.

Pandafox
  • 564
  • 1
  • 5
  • 18
  • Right on! Remember to mark the correct answers to your questions so people won't continue replying :) – Pandafox Jan 19 '16 at 08:54
0
var timeSecondString = String(format: "%02d", self.timesecond)
self.ScoreLabel.text = "\(self.score/60):\(self.timesecondString)"

I havent tested it but it should work

lukasbalaz7
  • 360
  • 2
  • 11