javalibgdxspritebatch

Libgdx SpriteBatch draws Actors, but not other Textures


It's been a while since I used LibGdx, so I feel like I'm just missing something obvious.

My render method in MyGdxGame looks like this, calling for the stage to draw itself (along with its actors), and then I try to draw a set of textures for debugging purposes.

@Override
    public void render()
    {        
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        StageManager.getCurrentStage().act();

        spriteBatch.begin();
        StageManager.getCurrentStage().draw();
        for(int i = 0; i < 100; i++) 
        {
            spriteBatch.draw(TextureManager.getPlayerTexture(), 50*i, 50*i);
        }
    }

The stage is drawn along with its one actor, but the other textures are not being drawn.

What I've tried: setting batch projection matrix off the stage camera(after calling update), making sure the texture coordinates should be visible.

The actor gets its texture from the same TextureManager.getPlayerTexture so I don't think its a texture issue.

What else should I check for to get the textures drawn as well?


Solution

  • Normally, your code would have caused a RuntimeException, because you're calling begin on the batch before drawing the stage. But since you actually have two sprite batches (one internal to the Stage because you didn't share the original with it), no actual errors occur.

    You're missing a call to spriteBatch.end() after drawing your texture(s). And the call to spriteBatch.begin() needs to move after stage.draw().

    And you should pass that sprite batch into the Stage constructor, because it is wasteful to have more than one sprite batch. Each sprite batch uses a fair amount of memory and compiles a shader.