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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With