【发布时间】:2014-10-07 10:26:43
【问题描述】:
总的来说,我是编程和 Spritekit 的新手,我有兴趣探索毫秒和帧率之间的关系,以及如何将更新函数用作两者之间的中介。
帧率与毫秒
本质上,帧率和时间之间的主要区别在于时间始终是一致的,而帧率则不是(由于密集的图形程序,它可能会下降)。但是,通常在 SKScene 的更新事件(每帧调用)期间检查和设置时间,所以我试图弄清楚如何正确计算时间,当你不知道一秒钟会有多少帧时.
示例
我目前正在研究太空射击游戏的更新事件,其中更新函数负责计算生成另一个外星人之前的时间间隔。你可以在这里查看完整代码:http://www.globalnerdy.com/2014/08/10/a-simple-shoot-em-up-game-with-sprite-kit-and-swift-part-one-last-things-first-the-complete-project/
// Called exactly once per frame as long as the scene is presented in a view
// and isn't paused
override func update(currentTime: CFTimeInterval) {
var timeSinceLastUpdate = currentTime - lastUpdateTime
lastUpdateTime = currentTime
if timeSinceLastUpdate > 1 {
timeSinceLastUpdate = 1.0 / 60.0
lastUpdateTime = currentTime
}
updateWithTimeSinceLastUpdate(timeSinceLastUpdate)
}
问题
我似乎无法弄清楚为什么 timeSinceLastUpdate 设置为 1.0 / 60。我知道这与帧速率和秒数之间的协调有关,但有人可以向我解释一下吗?另外,为什么我们可以使用小数?我认为时间间隔是 Int 类型的。
更重要的是,这样做的目的是为了防止游戏在帧速率下降时变慢吗?感谢阅读!
【问题讨论】:
标签: xcode time swift sprite-kit xcode6