Remove Actors from Stage?

Fabian König picture Fabian König · Mar 1, 2014 · Viewed 17.3k times · Source

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 Actor to 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?

Answer

kabb picture kabb · Mar 2, 2014

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.