【问题标题】:How do I change scenes in SceneKit?如何更改 SceneKit 中的场景?
【发布时间】:2020-11-26 09:51:10
【问题描述】:

假设我有游戏关卡:level_1.scn、level_2.scn、level_3.scn 等等。如果玩家已完成关卡,从当前关卡进入下一个关卡的最佳方式是什么?

这是我尝试过的:

extension GameViewController: SCNPhysicsContactDelegate {

    func physicsWorld(_ world: SCNPhysicsWorld, didEnd contact: SCNPhysicsContact) {
        guard let nodeA = contact.nodeA.name else { return }
        guard let nodeB = contact.nodeB.name else { return }

        if nodeA == "player" && nodeB == "finishLine" || nodeA == "finishLine" && nodeB == "player" {
            if let scene = SCNScene(named: "scenes.scnassets/level_2.scn") {
                sceneView.present(scene, with: .fade(withDuration: 0.25), incomingPointOfView: nil, completionHandler: nil)
            }
        }
    }
}

一旦这部分代码被执行,场景就会改变,但会有很大的延迟。我的关卡非常简单,我认为加载时间不应超过 1 秒。并且在场景改变后,我失去了对玩家的控制能力。

看来我做错了什么。因为控制台显示警告:

场景在渲染回调中被修改 另一个场景()。这是不允许的,并且 可能会导致崩溃。

如何创建正确的场景切换逻辑?

【问题讨论】:

    标签: ios swift scenekit


    【解决方案1】:

    我认为延迟是由于多次调用sceneView.present 造成的。 func physicsWorld 将在满足条件时运行多次,这可能会导致您提到的延迟。尝试使用变量来防止这种情况发生。

    【讨论】:

      猜你喜欢
      • 2020-04-06
      • 2019-11-06
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      相关资源
      最近更新 更多