2014-09-13 110 views
0

我是LibGDX的新手,而且我慢慢服用。當我運行應用程序時,它只是因爲錯誤而關閉。但我不確定問題是什麼。這是我有的按鈕的代碼。LibGDX - 如何讓我的按鈕工作?

@Override 
public void show() { 
    // Viewport Camera 
    camera = new PerspectiveCamera(); 
    viewport = new ExtendViewport(800, 480, camera); 

    stage = new Stage(new ExtendViewport(800, 840)); 
    Gdx.input.setInputProcessor(stage); 

    skin = new Skin(atlas); 
    atlas = new TextureAtlas(Gdx.files.internal("ui/button.pack")); 

    table = new Table(skin); 
    table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 

    textButtonStyle = new TextButtonStyle(); 
    textButtonStyle.up = skin.getDrawable("button.up"); 
    textButtonStyle.down = skin.getDrawable("button.down"); 

    buttonPlay = new TextButton("Play", textButtonStyle); 
    buttonPlay.setWidth((float) (Gdx.graphics.getWidth()/2.5)); 
    buttonPlay.setHeight(Gdx.graphics.getHeight()/6); 
    buttonPlay.setPosition((Gdx.graphics.getWidth()/2-buttonPlay.getWidth()/2), (float) (Gdx.graphics.getHeight()-(buttonPlay.getHeight())*2.5)); 
    buttonPlay.toFront(); 

    stage.addActor(buttonPlay); 
    table.debug(); 
    table.add(buttonPlay); 

} 

錯誤:

Exception in thread "LWJGL Application" java.lang.NullPointerException 
at com.badlogic.gdx.scenes.scene2d.ui.Skin.addRegions(Skin.java:102) 
at com.badlogic.gdx.scenes.scene2d.ui.Skin.<init>(Skin.java:88) 
at com.badlogic.gdx.screens.Menu.show(Menu.java:83) 
at com.badlogic.gdx.Game.setScreen(Game.java:61) 
at com.badlogic.gdx.screens.Splash.render(Splash.java:38) 
at com.badlogic.gdx.Game.render(Game.java:46) 
at com.dakotapederson.slingshotsteve.SlingshotSteve.render(SlingshotSteve.java:29) 
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206) 
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114) 

75行是顯示()方法是。

+0

什麼錯誤?請提供日誌。 – Veljko 2014-09-13 19:48:42

+0

他們你走了,我用表更新代碼,看看是否會修復東西 – Dakota 2014-09-13 19:54:12

回答

1

你的問題是,你的圖片初始化之前,你正在使用圖集初始化你的皮膚,圖集是空指針。你做了:

skin = new Skin(atlas); //Here atlas isn't loaded so exception occurs 
atlas = new TextureAtlas(Gdx.files.internal("ui/button.pack")); 

當它應該是:

atlas = new TextureAtlas(Gdx.files.internal("ui/button.pack")); // Load atlas first 
skin = new Skin(atlas); 
+0

謝謝,它解決了這個問題,但後來我得到一個「Missing LabelStyleFont Error」。我該怎麼做? – Dakota 2014-09-13 20:01:38

+1

你得到的問題可能是因爲你的uiskin地圖集沒有你正在製作的標籤的樣式,你從「ui/button.pack」加載的那個樣子聽起來像它只有一個按鈕的UI。您可以在https://github.com/libgdx/libgdx/wiki/Skin上閱讀有關UI外觀的更多信息。如果你想要一個完整的預先製作的UI地圖集,你可以在這裏下載一個:http://stackoverflow.com/questions/16182844/default-skin-libgdx – 2014-09-13 20:20:48

相關問題