创建按钮时LibGDX崩溃

时间:2014-11-09 16:41:45

标签: libgdx crash

我是LibGdx的新手。我一直在编程java一年,但今天我在LibGdx中遇到了一个Button错误。

导致它的原因,我该如何解决?

@Override
        public void render(float delta) {
            Gdx.gl.glClearColor(1, 1, 1, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            batch.begin();
            background.draw(batch);
            batch.end();
            }
        @Override
        public void show() {
            batch = new SpriteBatch();

 //   THE ERROR IS HERE|
    //                 V
            // BUTTON
            Skin skinLibgdx = new Skin();
            TextureAtlas ta =  new TextureAtlas(Gdx.files.internal("Button.pack"));
            skinLibgdx.addRegions(ta);

            Button button = new Button(skinLibgdx);
            button.addListener(new ClickListener() {
                @Override
                public void clicked(InputEvent event, float x, float y) {
                    // TODO Auto-generated method stub
                    System.out.println("ABC");
                }
            });

        }

        ...

    }

错误:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: No com.badlogic.gdx.scenes.scene2d.ui.Button$ButtonStyle registered with name: default
    at com.badlogic.gdx.scenes.scene2d.ui.Skin.get(Skin.java:145)
    at com.badlogic.gdx.scenes.scene2d.ui.Skin.get(Skin.java:132)
    at com.badlogic.gdx.scenes.scene2d.ui.Button.<init>(Button.java:50)
    at com.me.game.screen.MainMenu.show(MainMenu.java:53)
    at com.badlogic.gdx.Game.setScreen(Game.java:61)
    at com.me.game.screen.Splash$1.onEvent(Splash.java:64)
    at aurelienribon.tweenengine.BaseTween.callCallback(BaseTween.java:380)
    at aurelienribon.tweenengine.BaseTween.updateStep(BaseTween.java:521)
    at aurelienribon.tweenengine.BaseTween.update(BaseTween.java:424)
    at aurelienribon.tweenengine.TweenManager.update(TweenManager.java:166)
    at com.me.game.screen.Splash.render(Splash.java:35)
    at com.badlogic.gdx.Game.render(Game.java:46)
    at com.me.game.MainGame.render(MainGame.java:16)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

1 个答案:

答案 0 :(得分:1)

单独使用地图册对于按钮来说还不够。您必须为按钮提供default样式。这可以直接在皮肤上完成,也可以通过可以加载的JSON文件完成。查看https://github.com/libgdx/libgdx/wiki/Skin了解详细信息。

检查libgdx的测试,其中包含一个相当完整的示例(https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests-android/assets/data/uiskin.json)。

e.g。

...
com.badlogic.gdx.scenes.scene2d.ui.Button$ButtonStyle: {
  default: { down: default-round-down, up: default-round },
  toggle: { down: default-round-down, checked: default-round-down, up: default-round }
},
...

然后加载它,例如

Skin skinLibgdx = new Skin(Gdx.files.internal("data/uiskin.json"))

查看整个示例:https://github.com/libgdx/libgdx/blob/396126f3af63eb9d0d263afc59653e00e8083778/tests/gdx-tests/src/com/badlogic/gdx/tests/UITest.java