Green_qaue
Green_qaue

Reputation: 3661

Scene2d - absolute positioning

I want to add 3 numbers next to each other on the same row in a table, I also want there to be about 100 units padding between each number. Under these numbers are words that correspond to them, for example:

  0         0         0
Total      Wins     Losses

However, if the I use padding, ie table.add(actor).padLeft(100);, then changing the number from say 0 to 160 would also move the number to the right, since I'm using padding. example:

  160         0         0
Total      Wins     Losses

How can I avoid this behaviour? It must be a common scenario.

Upvotes: 0

Views: 432

Answers (2)

Steve Moseley
Steve Moseley

Reputation: 5877

What I would do is set all the cells in the table to be centred, by default, with a little padding on left and right, as follows:


    Table table1 = new Table();
    // Set defaults for all cells in table. Alignment and padding in this case.
    table1.defaults().align(Align.center).pad(0, 20, 0, 20);

    Label number1 = new Label("160", skin);
    Label number2 = new Label("0", skin);
    Label number3 = new Label("0", skin);
    table1.add(number1);
    table1.add(number2);
    table1.add(number3);
    table1.row();

    Label label1 = new Label("Total", skin);
    Label label2 = new Label("Wins", skin);
    Label label3 = new Label("Losses", skin);
    table1.add(label1);
    table1.add(label2);
    table1.add(label3);
    table1.row();

Try that.

Upvotes: 0

Luca Marzi
Luca Marzi

Reputation: 806

I suggest to create a custom class that extends the WidgetGroup. Inside this class you manage two labels, one for the number and one for the "label" of the number. Then you can easily set the relative position of the labels respect each other without keeping in mind where they are positioned inside the tabel. Maybe the constructor of this special class could accept a String for the "label" and the initial value of the number.

Tell me if the idea it's clear or if you need more info.

Luca

Upvotes: 0

Related Questions