SpriteKit - Creating a timer

前端 未结 5 1923
小鲜肉
小鲜肉 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:29

    In Swift usable:

    var timescore = Int()  
    var actionwait = SKAction.waitForDuration(0.5)
                var timesecond = Int()
                var actionrun = SKAction.runBlock({
                        timescore++
                        timesecond++
                        if timesecond == 60 {timesecond = 0}
                        scoreLabel.text = "Score Time: \(timescore/60):\(timesecond)"
                    })
                scoreLabel.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))
    

提交回复
热议问题