【问题标题】:Remove Actors from Stage?从舞台上移除演员?
【发布时间】:2014-04-03 00:11:30
【问题描述】:

我使用 LibGDX 并且只在我的游戏中移动相机。昨天我找到了一种在我的游戏中画出地面的方法。我正在尝试制作 Flappy Bird 的克隆,但在绘制在屏幕上移动的地面时遇到问题。在每次渲染调用中,我都会向Stage 添加一个新的Actor,但几次之后,绘图不再流动。每秒帧数下降得非常快。还有其他方法可以在游戏中绘制基础吗?

【问题讨论】:

  • 只是想在演员不可见时将其移除?

标签: java libgdx draw game-engine flappy-bird-clone


【解决方案1】:

如果我没看错,你的问题是一旦演员离开屏幕,他们仍在被处理并导致延迟,你希望他们被删除。如果是这种情况,您可以简单地遍历舞台中的所有演员,将他们的坐标投影到窗口坐标,并使用这些来确定演员是否在屏幕外。

for(Actor actor : stage.getActors())
{
    Vector3 windowCoordinates = new Vector3(actor.getX(), actor.getY(), 0);
    camera.project(windowCoordinates);
    if(windowCoordinates.x + actor.getWidth() < 0)
        actor.remove();
}

如果actor在窗口中的x坐标加上它的宽度小于0,则actor已经完全滚出屏幕,可以被移除。

【讨论】:

  • 它应该不会对性能造成太大损失,而且它通过移除演员获得的性能超过了弥补它。
  • 可能让演员在 act() 方法中自己做。容易得多,因为每个演员都可能有特定的“理由”将自己从游戏中移除。
【解决方案2】:

@kabb 对解决方案稍作调整:

    for(Actor actor : stage.getActors()) {
        //actor.remove();
        actor.addAction(Actions.removeActor());
    }

根据我的经验,在迭代 stage.getActors() 时调用 actor.remove()将打破循环,因为它正在从正在迭代的数组中删除参与者。

一些类似数组的类会抛出一个ConcurrentModificationException 对于这种情况作为警告。

所以...解决方法是告诉演员稍后Action

删除自己
    actor.addAction(Actions.removeActor());

或者...如果您出于某种原因等不及要删除演员,您可以使用SnapshotArray

    SnapshotArray<Actor> actors = new SnapshotArray<Actor>(stage.getActors());
    for(Actor actor : actors) {
        actor.remove();
    }

【讨论】:

    【解决方案3】:

    调用remove() 方法从其父级移除actor 的最简单方法。例如:

    //Create an actor and add it to the Stage:
    Actor myActor = new Actor();
    stage.addActor(myActor);
    
    //Then remove the actor:
    myActor.remove();
    

    【讨论】:

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