SpriteKit 的更新功能:时间 vs. 帧率 [英] SpriteKit's Update Function: time vs. framerate

查看:36
本文介绍了SpriteKit 的更新功能:时间 vs. 帧率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程和 Spritekit 的新手,对探索毫秒和毫秒之间的关系很感兴趣.帧率,以及如何将更新函数用作两者之间的中介.

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.

帧率 vs. 毫秒

本质上,帧率和时间之间的主要区别是时间总是一致的,而帧率不是(由于密集的图形程序,它可能会下降).但是,通常在 SKScene 的更新事件(称为每一帧)期间检查和设置时间,因此我试图弄清楚如何正确计算时间,当您不知道一秒钟内将有多少帧时.

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.

示例

我目前正在研究太空射击游戏的更新事件,其中更新功能负责计算生成另一个外星人之前的时间间隔.您可以在此处查看完整代码: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/

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/

// 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 类型的.

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.

更重要的是,这样做的目的是为了防止在帧率下降时游戏速度变慢吗?感谢阅读!

More importantly, is the purpose of this to keep the gameplay from slowing down during dips in framerate? Thanks for reading!

推荐答案

SpriteKit 不知道一秒钟内将多少帧,但它可以告诉你多少帧 自上一帧完成绘制以来,时间已经过去.如果您的游戏代码有任何依赖于时间的逻辑,这就是您想知道的.例如,您可能会执行诸如 position += velocity * timeSinceLastUpdate 之类的事情来移动精灵,因为您不需要成熟的物理引擎.

SpriteKit doesn't know how many frames are going to be in a second, but it can tell you how much time has elapsed since 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 尝试以每秒 60 帧的速度运行您的游戏.这意味着帧之间的时间是 1/60 秒(大约 0.01667 秒,或 16.67 毫秒).CFTimeIntervalDouble 的类型别名,因此它可以具有非整数值(并且需要,因为您使用的帧时间是几分之一秒).

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).

更重要的是,这样做的目的是为了防止在帧率下降时游戏速度变慢吗?

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 down if framerate dips significantly.

如果使用position += velocity * timeSinceLastUpdate 之类的代码在屏幕上移动一艘宇宙飞船,如果我们连续丢几帧会发生什么?timeSinceLastUpdate 的值会非常高,所以船会跳得很远,而不是移动一点.如果您的应用被暂停并稍后返回前台,这种情况尤其有可能发生.

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.

此代码检查自上一帧是否已经超过一秒,如果是,则选择继续游戏,就好像游戏一直以 60 fps 运行一样 - 避免跳跃的宇宙飞船问题.

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.

这篇关于SpriteKit 的更新功能:时间 vs. 帧率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆