3
votes

This question has been posted before, but i have problems with flickering sprites in libGdx. I have looked everywhere on stackoverflow, google, etc but nothing helped me.

The sprites only flicker when they move. It flickers less when i move them slower, but it flickers a lot when a little bit faster. This is very annoying and ofcourse unplayable.

This is my code:

package com.wouter.DuelArena;

import com.badlogic.gdx.Game;
import ...

public class DuelArena extends Game {

Sprite player;
float playerX;
float playerY;

boolean fireBool;
Sprite fireSprite;
Texture fireText;

OrthographicCamera cam;

Texture playerTexture;
SpriteBatch batch;

@Override
public void create() {      
    fireBool = false;
    fireText = new Texture("data/droplet.png");
    fireText.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    fireSprite = new Sprite(fireText);

    playerTexture = new Texture("data/bucket.png");
    playerTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    player = new Sprite(playerTexture);     
    player.setSize(70, 128);
    playerX = 0f;
    playerY = 0f;

    cam = new OrthographicCamera();
    cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    batch = new SpriteBatch();
}

@Override
public void dispose() {
    super.dispose();
}

@Override
public void render() {      

    Gdx.gl.glClearColor(1,  1,  1,  1);
    Gdx.gl.glClear(Gdx.gl10.GL_COLOR_BUFFER_BIT);

    cam.update();
    batch.setProjectionMatrix(cam.combined);

    batch.begin();


    player.draw(batch);
    input();
    update();

    batch.end();

    super.render();
}
private void update()
{
    player.setPosition(playerX, playerY);

    if (fireBool)
    {
        fireSprite.setX(fireSprite.getX() + 300 * Gdx.graphics.getDeltaTime());
        fireSprite.draw(batch);

        if (fireSprite.getX() > 1280)
            fireBool = false;
    }
}
private void input()
{
    if (Gdx.input.isKeyPressed(Keys.LEFT) && player.getX() > 0)
    {
        playerX -= 200f * Gdx.graphics.getDeltaTime();

    }
    if (Gdx.input.isKeyPressed(Keys.RIGHT) && player.getX() < 1280)
    {
        playerX += 200f * Gdx.graphics.getDeltaTime();
    }
    if (Gdx.input.isKeyPressed(Keys.SPACE) && !fireBool)
    {
        fireBool = true;
        fireSprite.setPosition(playerX, playerY + 35f * Gdx.graphics.getDeltaTime());
    }
}

@Override
public void resize(int width, int height) {
    super.resize(width, height);
}

@Override
public void pause() {
    super.pause();
}

@Override
public void resume() {
    super.resume();
}

}

Thanks beforehand!

1
I'm not sure it could cause this, but you should remove the super.render(); - noone
Really? Thanks for the reply, i'll try it tomorrow ;) - Wouter Standaert
But you're most probably right! I read in a tutorial you can't delete the super.render, but that was when our renderfunction was empty! So what i do is a double render, which means my screen is cleared twice! Thanks a lot! You made my day :) - Wouter Standaert

1 Answers

3
votes

The problem is the position of the call to the super.render:

super.render();

In a Game class, the super.render needs to be the first line in the code, not the last, like this:

@Override
public void render(){
    super.render(); //<-----

    Gdx.gl.glClearColor(1,  1,  1,  1);
    Gdx.gl.glClear(Gdx.gl10.GL_COLOR_BUFFER_BIT);
    cam.update();
    batch.setProjectionMatrix(cam.combined);

    batch.begin();
    player.draw(batch);
    input();
    update();
    batch.end();
}

It is actually very important to put it there if you override the render method in Game, because thats the one that calls the render of the current Screen. You aren't using Screens in this example, but if you are using Game you probably should (will :p).