2016-06-22 24 views
0

我正在使用Libgdx的Scene2D API通過UI進行構建,並且所有內容一直按預期工作到現在。我創建了一個SkinTextureAtlas和鏈接它們像這樣:Libgdx Scene2D表格背景圖片未顯示

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"); 

最後我添加了一個LabelTable

Label gameTitle = new Label("Nameless Project", skin, "title"); 
table.add(gameTitle).padBottom(10).row(); 

一旦我將Table添加到我的Stage中,只有其中的元素會顯示出nd不是背景圖片。我嘗試使用background(String s),setBackground(String s),background(Drawable d)setBackground(Drawable d)。這4種方法似乎都不適合我。

我也試過打電話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

你可以添加皮膚文件內容嗎? –

+0

你能向我們展示呈現舞臺的類嗎? – noone

+0

@WutipongWongsakuldej,我添加了請求的文件 – Chris

回答

1

終於找到了修復。在背景顯示之前,您需要撥打table.pack()。我打電話給table.pack(),但是把包放置在屏幕後面,所以我假設pack()使它消失了。