Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libgdx - how to add background image in stage?

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?

like image 961
rahul_raj Avatar asked Mar 20 '14 14:03

rahul_raj


2 Answers

A small example :

stage.act(Gdx.graphics.getDeltaTime());

stage.getBatch().begin();
stage.getBatch().draw(background, 0, 0, WORLD_WIDTH, WORLD_HEIGHT);
stage.getBatch().end();

stage.draw();
like image 156
RichieHH Avatar answered Oct 21 '22 14:10

RichieHH


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.

like image 14
Robert P Avatar answered Oct 21 '22 13:10

Robert P