1
\$\begingroup\$

I'm trying to set the player image inside the circle of the bars. I've tried to use stack, to position it topLeft but it does not work.

enter image description here

    Stack stack = new Stack();

    healthbar = new Image(new Texture(Gdx.files.internal("data/ui/healthbar.png")));
    playerLook = new Image(player.getFrame());
    stack.add(healthbar);
    stack.add(playerLook);

    Table table = new Table();
    table.top();
    table.setFillParent(true);

    table.add(healthbar).expandX().pad(10).left();
    table.add(playerLook).expandX().pad(10).left();

    stack.add(table);
    stage.addActor(stack);
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

First of all, we remove these two lines, because we don't want to add them separately:

table.add(healthbar).expandX().pad(10).left();
table.add(playerLook).expandX().pad(10).left();

Then we change this:

stack.add(table);
stage.addActor(stack);

to this:

table.add(stack);
stage.addActor(table);

Because we don't wish to add the table to the stack, we wish to add the stack to the table, and then we add the table to the stage. The result is this:

Stack stack = new Stack();

healthbar = new Image(new Texture(Gdx.files.internal("data/ui/healthbar.png")));
playerLook = new Image(player.getFrame());
stack.add(healthbar);
stack.add(playerLook);

Table table = new Table();
table.top();
table.setFillParent(true);
table.add(stack);

stage.addActor(table);

If you have any questions just leave a comment and I'll try to answer them :)

\$\endgroup\$
2
  • \$\begingroup\$ It half works, but the player image gets distorted. link \$\endgroup\$
    – Printer
    Jul 9, 2016 at 20:27
  • \$\begingroup\$ That's because it's trying to occupy the same amount of space as the other image. To solve this you can either make the images the same size, or split your health bar image into two parts - one for the circle and one for the bars - and then make a Stack with your player image and the circle. Last alternative, which I recommend, is to not use a Table for this, since table is inherently not made for stacking Actors. Instead use your own GUI class and render it on top of everything else. \$\endgroup\$
    – Charanor
    Jul 9, 2016 at 21:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .