Search code examples
androidopengl-eslibgdx

libgdx: Why do all my actors not render when I add an actor using Shaperenderer?


I am adding an actor to a group which causes all my actors to not show. The actors draw method is using Shaperenderer as shown below

@Override
public void draw(Batch batch, float parentAlpha) {
    super.draw(batch, parentAlpha);

    shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
    shapeRenderer.rectLine(ax, ay, bx, by, 5);
    shapeRenderer.end();
}

Whenever I remove the actor all the other actors in the group show without a problem. How can I fix this?


Solution

  • You can't interleave the begin/end of a SpriteBatch and a ShapeRenderer. When an actor's draw method is called, begin() has already been called on the SpriteBatch. So you can fix your draw() method like this:

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        batch.end();
        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        shapeRenderer.rectLine(ax, ay, bx, by, 5);
        shapeRenderer.end();
        batch.begin();
    }
    

    Also make sure you've set the projection matrix for the shape renderer.

    Note that you are causing an extra SpriteBatch flush for every actor that does this.