【问题标题】:LibGDX scene2D - Can swapActor be used to change position in table?LibGDX scene2D - 可以使用 swapActor 来改变表格中的位置吗?
【发布时间】:2014-05-03 13:23:55
【问题描述】:

我想交换 libGDX 表中两个标签的位置。我想我可以使用 swapActors 来做到这一点,但它没有解决问题。

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 是真的,但它仍然按要求说“AB”而不是“BA”。

还有另一种简单的方法来交换表格内的两个单元格或演员(意思是交换位置)吗?

【问题讨论】:

  • 难道你不能只使用 Vector2 来保存每个精灵的 x 和 y 位置,并在渲染之间简单地切换值吗?
  • 如果每个精灵坐在桌子上,我不会更改它们的 x 和 y。不知道会发生什么,因为桌子会为你设置它们的位置。
  • 您好,您找到解决方案了吗?

标签: libgdx scene2d


【解决方案1】:

看看 " API 文档中的 public Cell getCell(T actor) " 方法。

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

现在看看 Cell 文档:

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

您可以获取每个 Cell 实例中的 Actor 实例,然后使用常规的旧交换方法交换它们。使用 Cell.getActor 和 Cell.setActor。十分简单。

【讨论】:

    【解决方案2】:

    我遇到了与您完全相同的问题和方法,经过一段时间的修补后,我发现 table.swapActor 工作得非常好,如果您不调用 layout.swapActor。

    一旦您查看源代码,这就是有意义的。似乎 layout() 将先前移除的演员重新分配到其原始位置。

    哦,添加一个 scene2d 动作让这看起来真的很棒:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多