user3192180
user3192180

Reputation:

Blank fields in Libgdx's table

I have another problem with tables in Libgdx. I want to create a game board using it and give to user a possibility to add / remove elements from that board ( table ). So first of all I would like to know how to initialize that table so that it already has fields but invisible fields ( so that it's possible to get position of that field and put there new element ). So those are my 2 first questions :

  1. How to create those invisible fields ( all fields have fixed static size ) ?
  2. How to get position and change element ( actor ) in that field?

Hope someone can help me.

Upvotes: 3

Views: 1484

Answers (1)

Pinkie Swirl
Pinkie Swirl

Reputation: 2415

  1. You can create those by adding a cell to a table and set the size of the cell.
  2. You can then add a invisible button to that cell and if the button is clicked you can remove it and add a new element.

Here some example code:

// uiskin from libgdx tests
skin = new Skin(Gdx.files.internal("uiskin.json"));
table = new Table();

// invisible button style
final ButtonStyle bStyle = new ButtonStyle();

int colNum = 10, rowNum = 10;
for (int row = 0; row < rowNum; row++)
{
    for (int col = 0; col < colNum; col++)
    {
        final Button l = new Button(bStyle);
        l.addListener(new ClickListener()
        {
            @Override
            public void clicked(InputEvent event, float x, float y)
            {
                Cell<Button> cell = table.getCell(l);

                cell.clearActor();
                cell.setActor(new Label("test", skin));
            }
        });
        table.add(l).size(100, 100);
    }

    table.row();
}

stage = new Stage();

table.setFillParent(true);
stage.addActor(table);

Gdx.input.setInputProcessor(stage);

The skin files can be downloaded here: https://github.com/libgdx/libgdx/tree/master/tests/gdx-tests-android/assets/data

You need the uiskin.json, uiskin.atlas and uiskin.png file.

Upvotes: 4

Related Questions