libgdx TextButton不能与json一起使用

时间:2015-04-28 22:41:29

标签: java json libgdx

这是我的MenuScreen.java。这是一个类,可以从主类设置屏幕,并显示一个开始菜单。

package com.game.screens;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;

public class MenuScreen implements Screen
{   
    private Stage stage = new Stage();
    private Table table = new Table();

    private Skin skin = new Skin(Gdx.files.internal("skin.json"),
        new TextureAtlas(Gdx.files.internal("testingStuff.pack")));

    private TextButton buttonPlay = new TextButton("Play", skin),
        buttonExit = new TextButton("Exit", skin);


    @Override
    public void show() 
    {
        table.add(buttonPlay).row();
        table.add(buttonExit).row();

        table.setFillParent(true);
        stage.addActor(table);

        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void render(float delta) 
    {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act();
        stage.draw();
    }

    @Override
    public void resize(int width, int height) 
    {

    }

    @Override
    public void pause() 
    {

    }

    @Override
    public void resume() 
    {

    }

    @Override
    public void hide() 
    {
        dispose();
    }

    @Override
    public void dispose() 
    {
        stage.dispose();
        skin.dispose();
    }
}

我想在我的应用程序中添加一个简单的TextButton,但每当我运行它时,我都会收到涉及skin.json文件的错误。

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.SerializationException: Error reading file: skin.json
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:97)
at com.badlogic.gdx.scenes.scene2d.ui.Skin.<init>(Skin.java:82)
at com.game.screens.MenuScreen.<init>(MenuScreen.java:21)
at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:22)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: com.badlogic.gdx.utils.SerializationException: Error reading       file: skin.json
at com.badlogic.gdx.utils.Json.fromJson(Json.java:683)
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:95)
... 5 more
Caused by: com.badlogic.gdx.utils.SerializationException: Error parsing file: skin.json
at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:77)
at com.badlogic.gdx.utils.Json.fromJson(Json.java:681)
... 6 more
Caused by: com.badlogic.gdx.utils.SerializationException: Error parsing JSON on line 1 near: : {
"white": { "file": "skinTest.fnt" }
},
"com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle": {
"default": { "font": white }
}
at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:547)
at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:55)
at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:75)
... 7 more

2 个答案:

答案 0 :(得分:0)

您的*.json文件似乎有问题。看起来应该是这样的:

{
    "com.badlogic.gdx.graphics.g2d.BitmapFont": {
        "white": { "file": "skinTest.fnt" }
    },
    "com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle": {
        "default": { "font": white }
    }
}

我的*.fnt文件运行正常。也许你的*.fnt文件路径是错误的。通常使用该路径,它应位于android/assets/skinTest.fnt。如果要在桌面上测试应用程序,还应将工作目录设置为assets文件夹。 (我正在使用Android Studio)

答案 1 :(得分:0)

查看https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests-android/assets/data/uiskin.json

上的示例皮肤

我注意到样式类,属性和值没有引号。

将您的文件更改为以下内容可能会有效:

{
    com.badlogic.gdx.graphics.g2d.BitmapFont: {
        white: { file: skinTest.fnt }
    },
    com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
        default: { font: white }
    }
}