12

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?

3
  • Order is important, add your background first.
    – Chase
    Commented Mar 20, 2014 at 16:04
  • 3
    Simply draw your background, then draw your buttons. You can add the background as an Imageactor to the Stage if you want, or you can get the Stages SpriteBatch and draw with it.
    – Robert P
    Commented Mar 20, 2014 at 16:22
  • Possible duplicate of Libgdx background and foreground in single stage
    – Necreaux
    Commented Mar 19, 2017 at 21:49

3 Answers 3

14

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.

10

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();
-3

Here is some code I used to mimic a health bar. I used a ProgressBar.

    stage = new Stage();
    Texture bground = new Texture(Gdx.files.internal("pbBackground.png"));
    Table table = new Table();
    table.setFillParent(true);
    stage.addActor(table);
    font = new BitmapFont(Gdx.files.internal("gamefonts.fnt"));
    font.getData().scale(.1f);
    skin = new Skin();
    pixmap = new Pixmap(1, 1, Format.RGBA8888);
    pixmap.setColor(Color.WHITE);
    pixmap.fill();
    skin.add("white", new Texture(pixmap));
    LabelStyle lstyle = new LabelStyle();
    lstyle.font=font;
    Label mylabel = new Label("HP", lstyle);
    mylabel.setColor(Color.RED);
    mylabel.setPosition(1, 6);
    table.addActor(mylabel);
    textureBar = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("pb.png"))));
    barStyle = new ProgressBarStyle(skin.newDrawable("white", Color.DARK_GRAY), textureBar);
    barStyle.background = new TextureRegionDrawable(new TextureRegion(bground));
    barStyle.knobBefore=barStyle.knob;
    bar = new ProgressBar(0, 10, 0.5f, false, barStyle);
    bar.setPosition(1, 1);
    bar.setValue(0);
    bar.setAnimateDuration(2);
    table.addActor(bar);

This website may help you understand progressbars better. Also look at the docs for ProgressBar.

Not the answer you're looking for? Browse other questions tagged or ask your own question.