Libgdx Scene2D Table 背景图像未显示
Libgdx Scene2D Table background image not showing up
我正在使用 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");
最后我在 Table
中添加了一个 Label
:
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)
。 None 这 4 种方法似乎对我有用。
我也试过打电话给 pack()
和 setFillParent(true)
,但这两个都让我的 table 消失了。我已经检查了几乎所有 Google 个结果,但找不到其他人解决我的问题。
编辑
这是我的 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
}
}
}
以及整个class渲染舞台:
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() {
}
}
终于找到了解决方法。您需要先调用 table.pack()
才能显示背景。我正在调用 table.pack()
但将 table 定位在包之后的屏幕外,所以我假设 pack()
使它消失了。
我正在使用 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");
最后我在 Table
中添加了一个 Label
:
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)
。 None 这 4 种方法似乎对我有用。
我也试过打电话给 pack()
和 setFillParent(true)
,但这两个都让我的 table 消失了。我已经检查了几乎所有 Google 个结果,但找不到其他人解决我的问题。
编辑
这是我的 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
}
}
}
以及整个class渲染舞台:
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() {
}
}
终于找到了解决方法。您需要先调用 table.pack()
才能显示背景。我正在调用 table.pack()
但将 table 定位在包之后的屏幕外,所以我假设 pack()
使它消失了。