簡體   English   中英

從SpriteKit Swift的TimerLabel中刪除額外的零

[英]Removing an extra zero from a TimerLabel in SpriteKit Swift

我已經在Swift SpriteKit中創建了一個計時器函數,並將其分配給ScoreLabel。 當計時器處於活動狀態並且正在更新時,不需要的結果是... 0:01-0:09然后是0:010,當“秒針”從:09至:10。

一點幫助將是巨大的。

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])))

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

應該做的把戲!

%02d

意味着任何少於兩位十進制數字的數字將被補零。

var timeSecondString = String(format: "%02d", self.timesecond)
self.ScoreLabel.text = "\(self.score/60):\(self.timesecondString)"

我沒有測試過,但應該可以

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM