Lidjan
Lidjan

Reputation: 154

libgdx scene2dUI layer actors position

I have a problem with configurate position of buttons on my scenes layer. I'm trying to build menu, and fill it with buttons, but they are seems to ignore commands like setPosition() etc.

Code looks like that:

Table layerMenuControls = buildMenuControlsLayer();
stage.clear();
Stack stack = new Stack();
stage.addActor(stack);
stack.setSize(GameEngine.VIEWPORT_GUI_WIDTH, GameEngine.VIEWPORT_GUI_HEIGHT);
stack.add(layerBackground);
stack.add(layerMenuControls);


private Table buildMenuControlsLayer() {
        Table layer = new Table();
        singleplayerButton = new Button(swsSkin, "singlePlayerButton");
        singleplayerButton.setPosition((GameEngine.VIEWPORT_GUI_WIDTH / 2) - 64, 64);
        layer.add(singleplayerButton);

And there is nothing happening there. Buttons are ignoring these commands and position itselfs one by one horizontally.

Is there something i could forget about?

Upvotes: 1

Views: 994

Answers (2)

EssEllDee
EssEllDee

Reputation: 167

Tables in LibGDX set the position of their children relatively. So using setPosition won't work. Instead you can make use of Align

Firstly you should set the dimensions and position of the table so it knows where to align things. I assume you want the table to take up the full screen, if not you can change width and height accordingly. The following will do that

layer.setSize(GameEngine.VIEWPORT_GUI_WIDTH, GameEngine.VIEWPORT_GUI_HEIGHT); layer.setPosition(0,0);

Then you can position buttons etc. relative to this table. In order to set the button in the centre of the table you can use

layer.align(Align.center);

To position in the top center you can use

layer.align(Align.center|Align.top);

To stop all of your buttons adding in one line, you need to add multiple rows to the table. In between layer.add(singlePlayerButton); and whatever other buttons you are adding, you simply call layer.row();. This positions whatever you add to the table after this on a new row directly below it. In order to make things look a bit nicer you can use padding.

Hope this helps

Upvotes: 1

asherbret
asherbret

Reputation: 6018

From the wiki on libgdx's Table:

Table is a WidgetGroup that sets the position and size of its children using a logical table...

So basically, once you added singleplayerButton to layer, its position is set by its position in the Table. setPosition will be ignored in this case. If you want to set the position of the button manually, Table is not your best option. The whole point of Table is to relieve the user from needing to define the children's position and size.

Upvotes: 0

Related Questions