Search code examples
javaandroidlibgdx

LibGDX - The window is not being cleaned every frame


I am using LibGDX to build a simple game for android (based on flappy bird).

It happens that when the bird (actor) moves it keeps the old images on the screen, something like this:

enter image description here enter image description here

I have no idea why this is happening...

Here is my GameplayScreen class (that represents the game screen)

public class GameplayScreen extends ScreenAdapter{

    private FlappyBird _game;
    private OrthographicCamera _camera;

    private Stage _gameplayStage;
    private Bird _bird;
    private Image _background;


    public GameplayScreen(FlappyBird game){
        _game = game;

        _camera = new OrthographicCamera(FlappyBird.WIDTH, FlappyBird.HEIGHT);

        _gameplayStage = new Stage(new StretchViewport(FlappyBird.WIDTH, FlappyBird.HEIGHT, _camera));

        _background = new Image(Assets.background);
        _gameplayStage.addActor(_background);

        _bird = new Bird();
        _bird.setPosition(FlappyBird.WIDTH * 0.25f, FlappyBird.HEIGHT/2, Align.center);
        _gameplayStage.addActor(_bird);

    }

    @Override
    public void render(float delta){
        _gameplayStage.act();
        _gameplayStage.draw();
    }

    /*Resizes the camera when the screen is resized*/
    @Override
    public void resize(int width, int height){
        _camera.setToOrtho(false, width, height);
        Assets.batch.setProjectionMatrix(_camera.combined);
        _gameplayStage.getViewport().update(width, height, true);
    }
}

Does any one have a clue?


Solution

  •     //set the background color
        Gdx.gl.glClearColor(0, 0, 0, 0);
        //clear 
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    

    Add the code above in your render method. What this does is to clear the frames on the screen.