我正在制作一款 RTS 游戏,我希望有一个与《帝国时代》类似的 UI,并且 UI 位于底部。 我很难设置包含特定元素的表格的背景。
我尝试了很多事情,例如:
- setBackground() 和颜色
- setBackground() 使用皮肤中的图像
- 使用此处找到的自定义解决方案:Libgdx | Scene2d | Set background color of table?
但似乎没有任何效果。我认为问题是我尝试将背景颜色应用于保存其他表格的最后面的表格。我可以为前面的表格设置颜色,但不能为后面的主表格设置颜色。
这是我绘制用户界面的地方:
skin = new Skin(Gdx.files.internal("core/assets/skinComposerExport/uiskin.json"));
stage = new Stage(new ScreenViewport());
mainTable = new Table(skin);
unitInfoTable = new Table(skin);
unitSpecTable = new Table(skin);
className = new Label("Placeholder scnm", skin);
...
abilityCD = new Label("Placeholder acdn", skin);
mainTable.setPosition(Gdx.graphics.getWidth() / 2.0f, 0);
mainTable.center().bottom();
mainTable.setBackground("UI_background");
addLabelsToTable(unitInfoTable, className, hitPoints, mana, dmg, range);
addLabelsToTable(unitSpecTable, abilityName, abilityMana, abilityDmg, abilityRange, abilityCD);
mainTable.add(unitInfoTable).padRight(20);
mainTable.add(unitSpecTable);
stage.addActor(mainTable);
我希望屏幕的整个底部都是白色的(大约200px高并填充整个x轴)并在其上绘制元素,但我无法设置背景颜色。
最佳答案
您可以为表格指定大小,以便舞台知道如何正确渲染它。 (或者在外表上使用 setFillParent(true) )。通常,我会创建一个与舞台大小相同的 root
表,然后向该 root
表添加其他 scene2d 元素,并根据需要进行对齐。
这是一个使用 ShadeUI 的简单示例
@Override
public void create() {
stage = new Stage(new ScreenViewport());
batch = new SpriteBatch();
skin = new Skin(Gdx.files.internal("shadeui/uiskin.json"));
// create a root table, sized to our stage, and add it to the stage
Table root = new Table();
root.setBackground(skin.getDrawable("dialogRed"));
root.setSize(stage.getWidth(), stage.getHeight());
stage.addActor(root);
// now create our menu, bottom-aligned, filled to width, and add it to root
Table menu = new Table();
menu.setBackground(skin.getDrawable("dialogDim"));
root.add(menu).expand().bottom().fillX().height(50);
// add additional labels to the menu
menu.defaults().expandX().center().uniformX().uniformX();
menu.add(new Label("HP", skin));
menu.add(new Label("MP", skin));
menu.add(new Label("My Name", skin));
menu.add(new Label("My Class", skin));
}
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
stage.draw();
batch.end();
}
关于java - 如何在表格上设置背景颜色或图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55615964/