【问题标题】:Libgdx remove and add image from cellsLibgdx 从单元格中删除和添加图像
【发布时间】:2016-11-27 05:11:58
【问题描述】:

我有这张桌子:

table.add(image1).size(image1.getWidth(), image1.getHeight());
table.add().size(image1.getWidth(), image1.getHeight()).row();
table.add().size(image1.getWidth(), image1.getHeight());
table.add().size(image1.getWidth(), image1.getHeight());

我希望,当我单击空单元格时,从单元格中删除图像并添加到单击的单元格中。例如,当我单击第三个单元格时,从第一个单元格中删除 image1 并将其添加到第三个单元格。怎么办?

更新 2:

我写了这段代码,但是当我点击 cell1 时,image1 没有添加到表格中并添加到右侧屏幕上,点击 cell0 不起作用。

Group group = new Group();
    group.addActor(image1);
    group.addActor(image2);
    group.addActor(image3);
    root.add(group).size(16, 16);
    root.add(image4).size(image4.getWidth(), image3.getHeight()).row();
    root.add(image5).size(image5.getWidth(), image4.getHeight());
    root.add(image6).size(image6.getWidth(), image5.getHeight());

    stage.addActor(root);
    stage.setDebugAll(true);

    root.getChildren().get(1).addListener(new InputListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            root.getCells().get(1).clearActor();
            root.getCells().get(0).clearActor();
            root.getCells().get(0).setActor(image1);
            group.clear();
            group.addActor(image4);
            group.addActor(image2);
            group.addActor(image3);
            root.getCells().get(1).setActor(group);
            return true;
        }
    });

    root.getChildren().get(0).addListener(new InputListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            root.getCells().get(0).clearActor();
            root.getCells().get(1).clearActor();
            root.getCells().get(1).setActor(image4);
            group.clear();
            group.addActor(image1);
            group.addActor(image2);
            group.addActor(image3);
            root.getCells().get(0).setActor(group);
            return true;
        }
    });

【问题讨论】:

    标签: libgdx scene2d


    【解决方案1】:

    就个人而言,我会为每个表格单元格设置一个侦听器,该侦听器将清除具有“image1”的单元格,然后在单击的单元格中分配图像。比如:

    table.getChildren().get(0).addListener(new ClickListener(  // .get(0-3)
       Cell<Table> cell = table.getCell(image1);
       cell.clearActor();
       table.getCells().get(0).setActor(image1);               // .get(0-3)
    ));
    

    Read libgdx's table api page.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多