SpriteKit - Creating a timer

前端 未结 5 1937
小鲜肉
小鲜肉 2020-11-22 05:15

How can I create a timer that fires every two seconds that will increment the score by one on a HUD I have on my screen? This is the code I have for the HUD:



        
5条回答
  •  借酒劲吻你
    2020-11-22 05:22

    I've taken the swift example above and added in leading zeros for the clock.

        func updateClock() {
        var leadingZero = ""
        var leadingZeroMin = ""
        var timeMin = Int()
        var actionwait = SKAction.waitForDuration(1.0)
        var timesecond = Int()
        var actionrun = SKAction.runBlock({
            timeMin++
            timesecond++
            if timesecond == 60 {timesecond = 0}
            if timeMin  / 60 <= 9 { leadingZeroMin = "0" } else { leadingZeroMin = "" }
            if timesecond <= 9 { leadingZero = "0" } else { leadingZero = "" }
    
            self.flyTimeText.text = "Flight Time [ \(leadingZeroMin)\(timeMin/60) : \(leadingZero)\(timesecond) ]"
        })
        self.flyTimeText.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))
    }
    

提交回复
热议问题