xcode SpriteKit 的更新功能:时间 vs. 帧率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25296705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SpriteKit's Update Function: time vs. framerate
提问by Tyler Brown
I'm new to programming and Spritekit in general, and am interested in exploring the relationship between milliseconds & framerate, and how the update function is used as an intermediary between both.
我是编程和 Spritekit 的新手,我有兴趣探索毫秒和帧率之间的关系,以及如何将更新函数用作两者之间的中介。
Framerate vs. milliseconds
帧率与毫秒
Essentially, the main difference between framerate and time is that time is always consistant, while framerate is not (it could dip due to intensive graphics procedures). However, time is usually checked and set during SKScene's update event (which is called every frame), so I'm trying to figure out how time is correctly calculated, when you don't know how many frames are going to be in a second.
从本质上讲,帧率和时间之间的主要区别在于时间总是一致的,而帧率不是(由于密集的图形程序,它可能会下降)。但是,通常在 SKScene 的更新事件(称为每一帧)期间检查和设置时间,因此我试图弄清楚如何正确计算时间,当您不知道一秒钟内将有多少帧时.
Example
例子
I am currently looking at the update event of a space shooter game, where the update function is responsible for counting time intervals before spawning another alien. You can view the full code here: 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/
我目前正在查看太空射击游戏的更新事件,其中更新功能负责在产生另一个外星人之前计算时间间隔。您可以在此处查看完整代码:http: //www.globalnerdy.com/2014/08/10/a-simple-shoot-em-up-game-with-sprite-kit-and-swift-part-one-最后的事情首先完成的项目/
// 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)
}
Problem
问题
I can't seem to figure out why timeSinceLastUpdate is set to 1.0 / 60. I know it has to do with reconciling between framerate and seconds, but can someone explain this to me? Also, why are we allowed to use decimals? I thought time intervals were of type Int.
我似乎无法弄清楚为什么将 timeSinceLastUpdate 设置为 1.0 / 60。我知道这与帧率和秒之间的协调有关,但有人可以向我解释一下吗?另外,为什么我们允许使用小数?我认为时间间隔是 Int 类型的。
More importantly, is the purpose of this to keep the gameplay from slowing down during dips in framerate? Thanks for reading!
更重要的是,这样做的目的是为了防止在帧率下降时游戏速度变慢吗?谢谢阅读!
回答by rickster
SpriteKit doesn't know how many frames are going to bein a second, but it can tell you how much time has elapsedsince it finished drawing the previous frame. If your game code has any time-dependent logic, this is something you'll want to know. For example, you might be doing something like position += velocity * timeSinceLastUpdate
to move sprites around because you don't need a full-blown physics engine.
SpriteKit 不知道一秒钟内会有多少帧,但它可以告诉你从它完成前一帧绘制以来已经过去了多少时间。如果您的游戏代码有任何依赖于时间的逻辑,这就是您想知道的。例如,您可能正在执行诸如position += velocity * timeSinceLastUpdate
移动精灵之类的操作,因为您不需要成熟的物理引擎。
Framerate is frames per second: SpriteKit tries to run your game at 60 frames per 1 second. That means the time between frames is 1 / 60 seconds (about 0.01667 sec, or 16.67 milliseconds). CFTimeInterval
is a type alias for Double
, so it can have non-integer values (and needs to, since you work with frame times that are fractions of a second).
帧率是每秒帧数:SpriteKit 尝试以每秒 60 帧的速度运行您的游戏。这意味着帧之间的时间是 1 / 60 秒(大约 0.01667 秒,或 16.67 毫秒)。CFTimeInterval
是 的类型别名Double
,因此它可以具有非整数值(并且需要,因为您使用的帧时间是几分之一秒)。
More importantly, is the purpose of this to keep the gameplay from slowing down during dips in framerate?
更重要的是,这样做的目的是为了防止在帧率下降时游戏速度变慢吗?
You got the part about framerate dips right, but the effect is backward. You actually want to slow your gameplay downif framerate dips significantly.
你对帧率下降的部分理解正确,但效果却是落后的。如果帧率显着下降,您实际上希望减慢您的游戏速度。
If move a spaceship that across the screen by with code like position += velocity * timeSinceLastUpdate
, what's going to happen if we drop several frames in a row? The value of timeSinceLastUpdate
will be very high, so the ship will jump a large distance instead of moving just a bit. This is especially likely to happen if your app gets suspended and comes back to the foreground sometime later.
如果使用类似的代码在屏幕上移动一艘宇宙飞船,position += velocity * timeSinceLastUpdate
如果我们连续丢几帧会发生什么?的值timeSinceLastUpdate
会非常高,所以船会跳很远的距离,而不是仅仅移动一点。如果您的应用程序被暂停并稍后返回前台,这种情况尤其有可能发生。
This code checks to see if it's been more than a second since the last frame, and if so, elects to continue gameplay as if the game had been running at 60 fps - avoiding the jumping spaceship issue.
此代码检查自上一帧以来是否已经超过一秒,如果是,则选择继续游戏,就好像游戏以 60 fps 运行一样 - 避免跳跃的宇宙飞船问题。