Java 从舞台上移除演员?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22121467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:45:02  来源:igfitidea点击:

Remove Actors from Stage?

javalibgdxdrawgame-engineflappy-bird-clone

提问by Fabian K?nig

I use LibGDX and move only the camera in my game. Yesterday I founded a way to draw the ground in my game. I'm trying to make a clone of Flappy Bird, but I have problems with drawing the ground which is moving on the screen. In every render call I add a new Actorto the Stage, but after a few times the drawing is no more flowing. The frames per second sink very fast. Is there another way to draw ground in games?

我使用 LibGDX 并且只在我的游戏中移动相机。昨天我创建了一种在我的游戏中绘制地面的方法。我正在尝试制作 Flappy Bird 的克隆,但在绘制屏幕上移动的地面时遇到问题。在每次渲染调用中,我都会向 中添加一个新ActorStage,但几次之后,绘图就不再流畅了。每秒帧数下降得非常快。有没有另一种方法可以在游戏中取胜?

采纳答案by kabb

If I'm reading correctly, you're problem is that once actors go off the screen, they are still being processed and causing lag, and you want them to be removed. If that's the case, you can simply loop through all of the actors in the stage, project their coordinates to window coordinates, and use those to determine if the actor is off screen.

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

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();
}

If the actors x coordinate in the window plus it's width is less than 0, the actor has completely scrolled off the screen, and can be removed.

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

回答by Tony Ceralva

The easiest way to remove an actor from its parent it calling its remove()method. For example:

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

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

//Then remove the actor:
myActor.remove();

回答by strider

A slight tweak of the solution from @kabb:

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

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

From my experience, calling actor.remove()while iterating stage.getActors(), will break the loop, since it is removing the actor from the array that is being actively iterated.

从我的经验,号召actor.remove()而迭代stage.getActors()将打破循环,因为它是从正在积极地迭代数组移除演员。

Some array-like classes will throw a ConcurrentModificationExceptionfor this kind of situation as a warning.

一些类似数组的类会ConcurrentModificationException在这种情况下抛出一个警告。

So...the workaround is to tell the actors to remove themselves laterwith an Action

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

    actor.addAction(Actions.removeActor());


Alternatively...if you can't wait to remove the actor for some reason, you could use a SnapshotArray:

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

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