【问题标题】:GameplayKit Not Pausing When Scene Is Paused场景暂停时 GameplayKit 不暂停
【发布时间】:2016-07-25 13:44:36
【问题描述】:

我有一个 GKAgent2D 控制向目的地移动的 SKNode 敌人的移动。

当我暂停代理正在移动的 SKScene 时,敌方节点似乎继续移动而没有显示出这种移动。所有可见的动作都停止(也就是敌人停止移动)。如果我等待 5 秒然后取消暂停场景,就好像敌人从未停止移动,它突然出现在我没有暂停场景时应该出现的位置,并且它继续从那个点正常移动。

我查看了 Apple 为用户创建的名为 DemoBots 的示例演示游戏,他们通过简单地将 update:(NSTimeInterval *)currentTime 方法返回并实现了类似于我正在使用的“修复”。这是我的代码目前的样子:

- (void)update:(NSTimeInterval)currentTime {

    [super update:currentTime];

    if (_lastUpdateTime == 0) _lastUpdateTime = currentTime;

    float delta = currentTime - _lastUpdateTime;
    _lastUpdateTime = currentTime;

    // Stop updating if the `worldNode` is paused.
    if (worldNode.paused) { return };

    for (GKComponentSystem *componentSystem in componentSystems) {
         [componentSystem updateWithDeltaTime:deltaTime];
    }

}

但我无法更深入地了解他们正在做什么以确保在暂停发生时立即停止当前有效的 GKGoal 目标。我什至记录了 agentDidUpdate 方法,当我暂停场景时它会停止触发,所以我真的不确定它是如何继续移动的。

如果有人知道答案,请告诉我。谢谢!

更新:我什至尝试暂停设置为移动目标的各个节点:

- (void)didPauseScene {

    worldNode.paused = YES;

    /* 
     << Animations >>
     */

    for (OEnemy *enemy /*my subclass*/ in enemyArray) {
        enemy.paused = YES;
    }
}

但这仍然没有阻止GKGoals继续像他们一样不暂停场景..

更新 2:阻止代理冷却的唯一解决方案实际上是消除代理系统的目的:

if (worldNode.paused) {
    self.agentSystem = nil;
    return;
}

这是一个非常可悲的解决方案,因为我希望有一种更优雅/更合适的方式来停止目标,而不会将所有内容完全从场景中移除。然而,另一个问题是即使这样,在取消暂停时重置目标也会产生相同的问题,即跳转到没有发生暂停时它们所在的位置。

【问题讨论】:

  • 该代码是 your update 方法还是 Apple 的?我们需要看到你的来提供帮助。
  • 我已经更新了我的帖子来回答你的问题。这是我的代码。
  • Sprite Kit 暂停是错误的,我猜想这个错误会在游戏套件中出现。如果您的场景正在暂停和取消暂停,那么您所有的孩子都会暂停和取消暂停。覆盖场景中的 paused 变量,并让 setter 不设置属性的值
  • 我不太清楚你要覆盖 paused 变量是什么意思
  • override var paused:Bool{ get{ return super.paused} set{}}

标签: ios objective-c sprite-kit gameplay-kit


【解决方案1】:

如果另一个目标没有改变它,代理将沿着他们最后一个已知的有效速度继续。如果您希望代理停止,您可以尝试使用 GKGoal 的 goalToReachTargetSpeed:。

【讨论】:

    【解决方案2】:

    对于那些GKGoal goalToReachTargetSpeed: 不起作用的人。

    从实体中删除我的 MovementComponent 对我有用:

    let movementComponent = entity.component(ofType: MovementComponent.self)!
    entity.removeComponent(ofType: MovementComponent.self)
    componentSystem.removeComponent(moveComponent)
    

    当您恢复您的游戏时,请确保您创建一个新实例您的运动组件并将其再次添加到实体中:

    let movementComponent = MovementComponent()
    entity.addComponent(movementComponent)
    componentSystem.addComponent(moveComponent)
    

    使用 GKComponentSystem 更新运动组件时,还要确保相应地删除和添加运动组件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      相关资源
      最近更新 更多