0
votes

I'm trying to draw a texture. This works well as long as I do this in my Screen's render method:

public void render(float delta) {
    batch.begin();
    batch.draw(new Texture(Gdx.files.internal("badlogic.jpg")), 0f, 0f, 100, 100f);
    batch.end();
}

Image: working

However, if I render the Texture withing my Actor, the image is not displayed correctly. Instead of the image I only see one pixel which is far too big.

// The constructor of my Screen
public GameScreen(Main game) {
    Gdx.gl.glClearColor(1, 1, 1, 1f);
    Gdx.gl.glClear(Gdx.gl.GL_COLOR_BUFFER_BIT);

    viewport = new ExtendViewport(100f, 100f * (4f / 3f), 100f, 100f * (2f / 1f));
    viewport.apply();
    viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);

    stage = new Stage(viewport, batch);
    stage.addActor(new MyActor());
    //...
}


//The Actor-Class:
public class MyActor extends Actor {
@Override
public void draw(Batch batch, float parentAlpha) {
    batch.draw(new Texture(Gdx.files.internal("badlogic.jpg")), 0f, 0f, 100, 100f);
}
}

Image: not working

Thanks in advance :)

1
Don't instantiate a Texture in the draw or render methods. You are loading a new image and leaking the old one on every frame.Tenfour04
This was only for testing purpose, thanks :)SpoKaPh

1 Answers

0
votes

The issue was caused, because I used the used the SpriteBatch to draw the texture and for the ShapeRenderer. Solution:

batch.end(); //!!
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.line(...);
//...
shapeRenderer.end();
batch.begin();