【问题标题】:Libgdx Scene2D Table background image not showing upLibgdx Scene2D 表格背景图像未显示
【发布时间】:2016-06-22 02:57:03
【问题描述】:

我正在使用 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)。这 4 种方法似乎都不适合我。

我也试过打电话给pack()setFillParent(true),但这两个都让我的桌子消失了。我浏览了几乎所有的 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
     }
   }
}

以及整个班级渲染舞台:

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() {

    }

}

【问题讨论】:

  • 可以添加皮肤文件内容吗?
  • 你能告诉我们渲染舞台的类吗?
  • @WutipongWongsakuldej,我添加了两个请求的文件
  • 我的错,我应该问图集文件。文件"skins/uielements1.txt" 应该包含一个可绘制名称grey_button02
  • 确实如此,我在其他地方使用过地图集。当我将名称更改为不在图集中的纹理时,我得到一个错误

标签: java android libgdx scene2d


【解决方案1】:

终于找到了解决办法。您需要在背景显示之前致电table.pack()。我打电话给table.pack(),但把桌子放在屏幕外,所以我认为pack()让它消失了。

【讨论】:

    猜你喜欢
    • 2016-08-19
    • 2015-10-08
    • 1970-01-01
    • 2012-08-28
    • 2013-09-10
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多