我正在使用Libgdx的Scene2D API来构建UI,直到现在一切都按预期工作。我创建了Skin
和TextureAtlas
并将其链接为:
Skin skin = new Skin();
TextureAtlas uiElements1 = new TextureAtlas("skins/uielements1.txt");
skin.addRegions(uiElements1);
skin.load(Gdx.files.internal("skins/uiskin.json"));
然后我尝试创建Table
:
Table table = new Table(skin);
table.setWidth(stage.getWidth());
table.align(Align.center|Align.top);
table.setPosition(0, Gdx.graphics.getHeight());
table.padTop(30);
table.setBackground("grey_button02");
最后,我向Label
添加了Table
:
Label gameTitle = new Label("Nameless Project", skin, "title");
table.add(gameTitle).padBottom(10).row();
我将Table
添加到Stage
后,只会显示其中的元素,而不会显示背景图片。我尝试使用background(String s)
,setBackground(String s)
,background(Drawable d)
和setBackground(Drawable d)
。这四种方法似乎都不适合我。
我也试过调用pack()
和setFillParent(true)
,但这两个都让我的桌子消失了。我已经完成了几乎每一个谷歌搜索结果,而我无法找到其他人解决我的问题。
修改
这是我的uiskin.json文件
{
com.badlogic.gdx.graphics.g2d.BitmapFont: { default-font: { file: fonts/FutureThin.fnt } },
com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
default: { down: blue_button03, up: blue_button04, font: default-font },
},
com.badlogic.gdx.scenes.scene2d.ui.Window$WindowStyle: {
default: {
titleFont: default-font
}
},
com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle: {
title: {
font: default-font
}
}
}
整个阶段渲染阶段:
public class MainMenu implements Screen {
GameClass myGame;
Stage stage;
Skin skin;
Table table;
TextButton singlePlayer;
TextButton multiPlayer;
Label gameTitle;
public MainMenu(GameClass g) {
myGame = g;
// Create Skin, load texture Atlas, load skin
skin = new Skin();
TextureAtlas uiElements1 = new TextureAtlas("skins/uielements1.txt");
skin.addRegions(uiElements1);
skin.load(Gdx.files.internal("skins/uiskin.json"));
// Create stage
stage = new Stage(new ScreenViewport());
// Create Table
table = new Table(skin);
table.setWidth(stage.getWidth());
table.align(Align.center|Align.top);
table.setPosition(0, Gdx.graphics.getHeight());
table.padTop(30);
table.setBackground("grey_button02");
// Game Title
gameTitle = new Label("Nameless Project", skin, "title");
table.add(gameTitle).padBottom(30).row();
// Single Player button
singlePlayer = new TextButton("Single Player", skin, "default");
singlePlayer.addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y) {
myGame.setScreen(new WorldTesting(myGame));
}
});
table.add(singlePlayer).width(300).padBottom(30).row();
// Multiplayer Button
multiPlayer = new TextButton("Multiplayer", skin, "default");
multiPlayer.setWidth(350);
multiPlayer.addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y) {
// Change Scenes
}
});
table.add(multiPlayer).width(300).padBottom(30).row();
stage.addActor(table);
Gdx.input.setInputProcessor(stage);
}
public void show() {
}
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
public void hide(){
}
public void resize(int x, int y) {
}
public void pause() {
}
public void resume() {
}
public void dispose() {
}
}
答案 0 :(得分:1)
终于找到了修复。在背景显示之前,您需要致电table.pack()
。我正在打电话给table.pack()
,但是在包装后将桌子放在屏幕外,所以我假设pack()
让它消失了。