Swift GameplayKit 在不暂停场景的情况下暂停 GKAgent

Swift GameplayKit pause GKAgent without pausing scene

暂停 GKAgent 的最佳方法是什么?

我的游戏在某些关卡中使用了一些代理,我需要在我的游戏 paused/gameOver 时暂停它们。

我不会在我的游戏中暂停整个 SKScene,而是暂停一个 worldNode,因为即使在游戏暂停时,它也能让我更灵活地显示 spriteKit 内容。

我正在使用

 updateWithDeltaTime...

更新代理行为并相应移动它们的方法。

我考虑过停止更新方法,但代理仍会移动到他们最后已知的 GKGoal。

目前我找到的最佳解决方案是在我的游戏暂停时将代理 speed/maxSpeed 设置为 0。我在这里遇到的问题是,恢复后将速度重置为代理以前的速度有点痛苦,尤其是在使用多个具有自己行为的代理时。它们似乎也消失了,而不是在恢复时重新出现。

据我所知,没有agent.paused方法或类似的东西。

在不暂停 SKScene 本身的情况下暂停代理的好方法是什么?

感谢您的帮助和建议。

你有没有把行 if worldNode.paused { return } 放在你的 GameScene 的 update 中?

这对我有用

游戏场景:

override func update(currentTime: CFTimeInterval) {

        super.update(currentTime)

        // Don't perform any updates if the scene isn't in a view.
        guard view != nil else { return }

        let deltaTime = currentTime - lastUpdateTimeInterval
        lastUpdateTimeInterval = currentTime

        /*
        Don't evaluate any updates if the `worldNode` is paused.
        Pausing a subsection of the node tree allows the `camera`
        and `overlay` nodes to remain interactive.
        */
        if worldNode.paused { return }

        // Don't perform any updates if the scene isn't in a view.
        guard entityManagerGame != nil else { return }

        entityManagerGame.update(deltaTime)

    }

我想我现在找到了一个似乎有效的解决方案。

首先,我将在游戏暂停时停止 udapteDeltaMethods。

然后我查看所有实体并将代理委托设置为 nil

  for entity in baseScene.entityManager.entities {
        if let soldier = entity as? BossWorld3Soldiers {
            soldier.agentComponent.delegate = nil
        }
    }

当我恢复游戏时,我称之为

  for entity in baseScene.entityManager.entities {
        if let soldier = entity as? BossWorld3Soldiers {
            let action1 = SKAction.waitForDuration(0.5)
            let action2 = SKAction.runBlock({ soldier.resetAgentDelegate() })
            baseScene.runAction(SKAction.sequence([action1, action2]))
        }
    }

resetAgentDelegate() 方法只是我的实体中的一种便捷方法类,用于重置代理委托

 func resetAgentDelegate() {
    self.agentComponent.delegate = self
 }

在重置代理委托之前,我在恢复时使用了轻微的延迟,因为没有延迟,enties/agents 似乎在恢复他们的 GKGoals 之前做了几秒钟的大量 jump/dissappear。