Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LibGDX scene2D - Can swapActor be used to change position in table?

Tags:

libgdx

scene2d

I want to swap the positions of two labels inside a table of libGDX. I thought I may use swapActors to do this, but it doesn't do the trick.

private Stage stage;
private Table table;

public MainScreen() {

    stage = new Stage(new FitViewport(800, 480));
    table = new Table(SkinManagerImpl.getDefaultSkin());
    table.add("A").center();
    table.add("B").center();
    table.setFillParent(true);
    boolean succes=table.swapActor(0, 1);
    Gdx.app.log("", "success:"+succes);
    table.layout();
    stage.addActor(table);
}

@Override
    public void render(float delta) {
        // TODO Auto-generated method stub
        super.render(delta);
        stage.draw();
    }

success is true, but it still says "AB" and not "BA" as wanted.

Is there another simple way to swap two cells or actors (meaning swap positions) inside a table?

like image 340
angelnoir Avatar asked Oct 20 '22 09:10

angelnoir


1 Answers

Have a look at the " public Cell getCell(T actor) " method in the API docs.

http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/Table.html#getCell-T-

Now have a look at the Cell docs:

http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/Cell.html

You can get the Actor instance that is in each Cell instance, and then swap them using a regular old swap method. Use Cell.getActor and Cell.setActor. Easy peasy.

like image 152
Scuba Steve Avatar answered Jan 02 '23 20:01

Scuba Steve