libgdx - how to add background image in stage?

rahul_raj picture rahul_raj · Mar 20, 2014 · Viewed 9.9k times · Source

I am learning libgdx but i am stuck at a point..

I have added a button in my stage , now i want to add a image in the stage so that the image looks as the background image to the button.i mean to say that the button should lie on the image. I have been looking tutorials but not been able to do that.

How can it be done? any help?

Answer

Springrbua picture Springrbua · Nov 3, 2014

The only thing you need to do is to draw the background, before drawing the Buttons.
There are a few possible ways to do that:
- You can add the background as an Image (subclass of Actor) to the Stage and use the z-index to make sure it is drawn as a background.
- You can get the Stages SpriteBatch (stage.getBatch()) and use one of its draw methods to draw the background, before calling stage.draw()

You could also use another SpriteBatch, but i don't recommend it, as it is a pretty "heavy" object and it is just not neccessaty in this case.
I guess, before calling stage.draw() you need to call spritebatch.end(), for the SpriteBatch you used to draw the background.