swift - 如果我将其留空,为什么我的更新功能会占用 85% 的时间?

标签 swift sprite-kit instruments swift4 skscene

我在分析我的应用程序时有点困惑。我注意到随着它的进行,CPU 使用率会增加,但我不确定为什么。即使当玩家空闲时,似乎有 72% 的 CPU 使用率似乎来自 [SKScene _update:] 我的更新功能是空白的,因为我的游戏是回合制的,所以为什么会有这么多在这里?

如有任何提示,我们将不胜感激。

enter image description here

如果需要说的话,这是我的更新方法:

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
}

附加信息:

主游戏 map 是一个基于十六进制的 SKTileMapNode,其 UI 使用 SKCameraNode 绘制。 UI 由大约 30 个节点组成,包括 SKLabelNodeSKSpriteNode 有一个 AI 在用户每次结束回合时在单独的线程上运行。当 AI 运行时,主线程 CPU 使用率立即下降,如上图所示。这很奇怪,因为 AI 只运行 GKMinMaxStrategist bestMoveForPlayer() 函数。

最佳答案

您可能正在手动调用 update(_:),而后者又会再次调用 update(_:),您应该覆盖这个更新场景的方法,即使你有一个空的实现。确保您已设置场景的 SKSceneDelegate

Do not call this method directly; it is called exactly once per frame, so long as the scene is presented in a view and is not paused. By default, this method does nothing. Your scene subclass should override this method and perform any necessary updates to the scene.

您也可以手动暂停/恢复 View :

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    guard let view = view else {
        return
    }

    view.paused = !view.paused
}

关于swift - 如果我将其留空,为什么我的更新功能会占用 85% 的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47965478/

相关文章:

ios - 自定义几何网格——如何进行三角剖分以使其看起来平滑

swift - DispatchQueue 同步块(synchronized block)与异步 block

iOS 混合 Spritekit 和 UIKit

ios - 长时间触摸时增加跳跃高度,直到最大高度

ios - 传递 self 并在没有保留周期的闭包中返回它

xcode - 根据最近的信标播放来自 url 的视频

ios - 如何让 SCNTransaction 结束

ios - 在 uiwebview 中渲染 svg 会导致内存压力(和应用程序崩溃)

objective-c - CGImage、NSArray 和内存

ios - 为什么我会漏水?