Search code examples
javaandroid-studiolibgdxactorspritebatch

SpriteBatch set below the level of the Actor in libgdx


I'm making a game in libgdx , and I've added different Actor forming my scene . Currently however, I added a texture:

//other variables:
Texture texture; int sourceX = 0;

//create() method
texture = new Texture(Gdx.files.internal("background.png"));
texture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
...other code...
stage.addActor(actorMen);   

and in the render() method:

//render() method  
stage.act(Gdx.graphics.getDeltaTime());
stage.draw(); 
sourceX+=10;      
batch.begin();          
batch.draw(texture, 0, 0, sourceX, 0, (int) background.getWidth(), (int) background.getHeight());
batch.end();

everything works , repeat the effect goes to great ... but my problem is that I covers all Actor as if their z- index was lower than the background . And ' you can indicate the spritebatch to remain lower than the actors ?

EDIT SOLUTION set render() method like this:

//render() method  

sourceX+=10;      
batch.begin();          
batch.draw(texture, 0, 0, sourceX, 0, (int) background.getWidth(), (int) background.getHeight());
batch.end();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw(); 

Solution

  • Solution, move the stage.act() and stage.draw() on the end the method render:

    //render() method  
    
    sourceX+=10;      
    batch.begin();          
    batch.draw(texture, 0, 0, sourceX, 0, (int) background.getWidth(), (int) background.getHeight());
    batch.end();
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();