UpdateTimer
是一个简单的struct
,它跟踪游戏场景循环中常用的时间变量。它目前公开:
update()
的次数)UpdateTimer
还包括一个圈速计时器,可以非常容易地跟踪任意事件(例如,不必在每个update()
周期发生的事件)。在您场景循环的update()
方法期间,timeSinceLastLap
将设置为自上次调用lap()
以来经过的秒数(如果尚未调用,则为自第一次调用update()
以来经过的秒数)。在任何您要监控的事件发生时调用lap()
,然后timeSinceLastLap
将重置为0并重新开始计数。
class MyScene: SKScene
{
let spawnMonsterInterval: NSTimeInterval = 5
private var updateTimer = UpdateTimer()
func update(currentTime:NSTimeInterval) {
updateTimer.update(currentTime)
println("seconds since last update() = \(updateTimer.timeSinceLastUpdate)")
println("seconds since first update() = \(updateTimer.timeSinceFirstUpdate)")
println("seconds since monster was spawned = \(updateTimer.timeSinceLastLap)")
println("number of times update() has been called = \(updateTimer.frameCount)")
if updateTimer.timeSinceLastLap >= spawnMonsterInterval {
spawnMonster()
updateTimer.lap() // resets `timeSinceLastLap` to zero
}
}
}
bryn austin bellomy < [email protected] >