2016-06-19 35 views
1

我有发起与3个细胞的表填充像这样一类:使用LibGDX中的ImageButton更新表格单元格?

[leftButton] [ImageButton的] [rightButton],创建


的ImageButton &加入characterSelection数组:

Texture characters2 = TrafficGame.res.getTexture("chevy"); 
characterImageStyle = new ImageButton.ImageButtonStyle(); 
car = new TextureRegion(characters2, 32, 32); 
characterImageStyle.imageUp = new TextureRegionDrawable(new TextureRegion(car)); 
characterImageChevy = new ImageButton(characterImageStyle); 
characterSelection.add(characterImageChevy); 

玩家选择开始时加入表格:

table.add(characterSelection.pop()); 

现在,当用户点击左/右按键,moveLeft /右()被调用:

public void moveLeft(){ 
    table.getChildren().get(1).clear(); 
} 

这其中,我坚持。它显然清除了单元格,但我不知道如何从数组中添加ImageButton。

回答

1

表不支持将角色交换到同一个单元格中。您应该隐藏它,而不是清除图像按钮。当您准备好显示其他图像时,请更改图像并显示它。

characterImageChevy.hide(); 

((TextureRegionDrawable)characterImageChevy.getStyle().imageUp).setTexture(...); 
characterImageChevy.show(); 
+0

感谢您的快速解释。我正在编码,就好像我必须为每个字符创建一个ImageButton,我应该想到它只是一个TextureRegion的占位符,我想。 '((TextureRegionDrawable)characterImage.getStyle()。imageUp).setRegion(characterSelection.peek());' 对我来说就像一个魅力。 – Spacemudd

+0

表支持在同一单元中交换元素,但不直接使用“交换”方法。但是你可以得到演员的细胞,然后清除它,并设置一个新的演员。我在下面写了一个单独的答案来证明它。 – middlehut

+1

你说得对。我误解了你不能插入单元格的事实。 – Tenfour04

0

您可以直接更改Scene2D单元格的内容。您可以通过要求表格返回特定演员的单元格来获取单元格。之后,您可以清除单元格并为其内容设置新的主角。

// first, grab the Actor that you wish to replace 
ImageButton actor = characterImageChevy; // you know where to get it from 

// next, take the Cell where it's at 
Cell<Table> cell = table.getCell(characterImageChevy); 

// remove the actor 
cell.clearActor(); 

// set new Actor 
cell.setActor(characterImageChevy); 
相关问题