【问题标题】:Is there a way to read/get fps in spritekit swift有没有办法在 spritekit swift 中读取/获取 fps
【发布时间】:2015-11-20 03:13:06
【问题描述】:

有没有快速获取fps值的方法?

不,我不是说改变 frameInterval 不,我不是这个意思

skView.showsFPS = true

我想获取 fps,以便我的应用程序等到 fps 达到 60 后再开始游戏。出于某种原因,我的应用偶尔会以 40fps 的速度“卡住”5 秒钟,然后慢慢回升至 60。这通常发生在我召唤 ControlCenter 时。

【问题讨论】:

  • 这没有意义。您需要弄清楚为什么您的应用需要一段时间来预热,而不是通过检查 FPS 值来解决问题。
  • 我自己想弄清楚。我已经浏览了有关 stackoverflow 的多个类似问题,但对于它为什么这样做没有真正明确的答案。适用于其他用户的解决方案不适用于我的情况。我也更喜欢知道它为什么这样做,而不是寻找解决方法,但是在调试了很多天之后,我仍然没有任何线索。
  • 好问题。在游戏开发中,需要随时了解您的 FPS 是完全正常的。

标签: swift sprite-kit frame-rate


【解决方案1】:

你可以自己计算。

在你的场景中做这样的事情:

var lastUpdateTime: TimeInterval = 0

func update(currentTime: TimeInterval) {
    let deltaTime = currentTime - lastUpdateTime
    let currentFPS = 1 / deltaTime
    print(currentFPS)

    lastUpdateTime = currentTime
}

【讨论】:

    【解决方案2】:

    使用SKViewDelegate(取自开发者文档,仅添加了 FPS 属性)还有另一种方法可以让视图本身的 FPS 独立于呈现的场景:

    
    class ViewController: NSViewController, SKViewDelegate {
        
        let spriteView: SKView = SKView()
        
        override func viewDidLoad() {
            self.spriteView.delegate = self // Set the view's delegate to the ViewController.
        }
    
        let targetFps: TimeInterval = 60 // Set to your preferred fps
        var lastRenderTime: TimeInterval = 0
        var fps: TimeInterval = 0.0 // Access this property to get the view's fps
        
        // Delegate method:
        public func view(_ view: SKView, shouldRenderAtTime time: TimeInterval) -> Bool {
            
            if time - lastRenderTime >= 1 / targetFps { // Check if the scene should render.
                
                // Determine the fps with the time since the last render:
                self.fps = 1 / (time - lastRenderTime)
                // Set the last render time to the current time:
                self.lastRenderTime = time
                // Let scene know that it should render the next frame.
                return true
    
            }
            else {
                return false
            }
            
        }
    }
    
    

    【讨论】:

      【解决方案3】:

      gameView.showsStatistics = true

      【讨论】:

        猜你喜欢
        • 2020-05-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-19
        • 1970-01-01
        • 1970-01-01
        • 2020-02-17
        • 2022-12-10
        • 1970-01-01
        相关资源
        最近更新 更多