我试图以编程方式制作一个以秒为单位的计时器,并且能够在游戏结束时停止。
在顶部我定义了这个变量
var timeCount = 0
var timerRunning = true
var timer = NSTimer()
然后在它下面的这个函数
func counting() {
timeCount++
scoreLabelNode = SKLabelNode(fontNamed:"[z] Arista Light")
scoreLabelNode.text = String(timeCount)
scoreLabelNode.zPosition = 100
scoreLabelNode.fontSize = 500
scoreLabelNode.alpha = 0.03
scoreLabelNode.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/4)
self.addChild(scoreLabelNode)
}
我还添加了这个来启动didMoveToView中的计时器
if timerRunning == false {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("counting"), userInfo: nil, repeats: true)
timerRunning = true
}
counting()