2014-09-13 90 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