哦好吧...经过几乎一天的尝试后,找不到它所以...就在这里。我正在使用 scene2d。
我的问题是这个,我有一个名为“Tile”的类,它扩展了 Image 类(扩展了小部件),并且其中有一个大小为 128x128 的纹理,但是当我尝试将其添加到表中(小部件组),我的图 block 消失了。
编辑:我刚刚对表格进行了大小检查,并验证了在表格内添加大小为 128x128 的图 block 后,表格显示的大小为 0x0。 ..也许这里出了什么问题?
我已经尝试过:
- 使用 addActor() 而不是 add(),Tile 显示完美,但无法按照我需要的方式使用表格...
- 使用 add() 添加其他 Actor ,他们就会出现。
- 将图 block 添加到其他参与者的中间,会发生的情况是图 block 根本不出现,甚至不显示为空白(如果向表中添加空对象就会发生这种情况)。<
- 查看图 block 的首选尺寸是否有问题,128x128 是可以的。
OBS:我奇怪地观察到的一件事是,当我尝试在 Tile 构造函数中使用 setDrawable() (对纹理->TextureRegion->TextureRegionDrawable 进行适当的转换)时,我无法以任何方式绘制它,我只是可以通过在构造函数中调用 super(Texture) 并观察 Image(Drawable) 构造函数来使其获取纹理,我碰巧注意到它在第一次设置可绘制对象时做了一些其他事情,但只是为了第一次... 而且...即使使用 super(Texture),在将单元格添加到表格中时它也不会显示... 也许问题就在这里?我不知道...
我的问题是:
- 问题是什么?
- 如果了解问题不足以解决问题,我该怎么办?
编辑2:根据要求,以下是相关代码:
public class Tile extends Image{
// Tiles
private Texture unselected;
private Texture selected;
// InputListener for detecting mouse over
private class MouseOver extends InputListener{
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
setDrawable(new TextureRegionDrawable(new TextureRegion(selected)));
}
}
private class MouseOut extends InputListener{
public void exit(InputEvent event, float x, float y, int pointer, Actor fromActor) {
setDrawable(new TextureRegionDrawable(new TextureRegion(unselected)));
}
}
public Tile(){
super(TextureFactory.unselectedTile());
unselected = TextureFactory.unselectedTile();
selected = TextureFactory.selectedTile();
this.addListener(new MouseOver());
this.addListener(new MouseOut());
}
}
该图 block 有一个监听器,当我将其鼠标悬停在其上方时,它会更改其纹理。我删除了这些监听器进行测试,但错误仍然发生,所以...不是它们。
实现 Tile 的屏幕(有些内容被注释了,因为我希望像那样使用它......)
public class CombatScreen extends AbstractScreen{
private Table board;
public CombatScreen(SpaceGameMain game, int boardSize) {
// Passing game to super class and getting class name for tracking
super(game);
this.className = this.getClass().getSimpleName();
// Creating tile board
board = new Table();
/*
for(int i = 0; i < boardSize; i++)
{
for(int j = 0; j < boardSize; j++){
board.add(new Tile());
}
board.row();
}
*/
}
@Override
public void show(){
super.show();
board.add(new Tile());
mainStage.addActor(board);
}
public void resize(int width, int height){
super.resize(width, height);
board.setPosition((Gdx.graphics.getWidth() - Tile.width)/2, (Gdx.graphics.getHeight() - Tile.height)/2);
}
}
表格设置在屏幕中间进行测试。
最佳答案
在 badlogic 论坛的 Tanmay 和 NateS 的帮助下找到了解决方案,他们引导我进一步了解 TableLayout。
在这种情况下,子级的原始大小不会影响“板”布局。您需要做的是使用 TableLayout 设置子级的 SIZE(而不是 prefSize),因此工作代码如下所示:
board.add(new Tile()).size(128, 128);
就是这样,关于如何使用表格的一些简单误解。
谢谢大家。
关于java - 表格未显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20530528/