LibGDX-如何让我的按钮工作?
我是LibGDX新手,我正在慢慢地学习。当我运行应用程序时,它只是由于错误而强制关闭。但我不确定问题出在哪里。这是我的按钮代码LibGDX-如何让我的按钮工作?,libgdx,Libgdx,我是LibGDX新手,我正在慢慢地学习。当我运行应用程序时,它只是由于错误而强制关闭。但我不确定问题出在哪里。这是我的按钮代码 @Override public void show() { // Viewport Camera camera = new PerspectiveCamera(); viewport = new ExtendViewport(800, 480, camera); stage = new Stage(new ExtendViewport
@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)
线程“LWJGL应用程序”java.lang.NullPointerException中的异常
位于com.badlogic.gdx.scenes.scene2d.ui.Skin.addRegions(Skin.java:102)
位于com.badlogic.gdx.scenes.scene2d.ui.Skin.(Skin.java:88)
位于com.badlogic.gdx.screens.Menu.show(Menu.java:83)
在com.badlogic.gdx.Game.setScreen上(Game.java:61)
位于com.badlogic.gdx.screens.Splash.render(Splash.java:38)
位于com.badlogic.gdx.Game.render(Game.java:46)
位于com.dakotapederson.slingshotsteve.slingshotsteve.render(slingshotsteve.java:29)
位于com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
位于com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
第75行是show()方法所在的位置。您的问题是,在初始化atlas之前,您正在使用atlas初始化皮肤,atlas是空指针。你已经做到了:
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);
错误是什么?请提供日志。如果你去,我会用一个表更新代码,看看这是否能解决问题。Hanks,它解决了这个问题,但是我得到了一个“缺少LabelStyleFont错误”。我该怎么办?你遇到的问题可能是因为你的uiskin图集没有你正在制作的标签的样式,你正在从“ui/button.pack”加载的标签听起来好像只有一个按钮的ui。有关UI外观的更多信息,请访问:。如果您想要一个完整的预制UI atlas,可以在此处下载: