简体   繁体   English

libgdx TextButton不适用于json

[英]libgdx TextButton not working with json

Here is my MenuScreen.java. 这是我的MenuScreen.java。 This is a class that the screen can be set as from the main class and will display a start menu. 可以从主班级将屏幕设置为该班级,并显示开始菜单。

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();
    }
}

I want to add a simple TextButton to my application, but whenever I run this, I get this error involving the skin.json file. 我想向应用程序中添加一个简单的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

Seems like a problem with your *.json file. *.json文件似乎有问题。 It should look something like that: 它看起来应该像这样:

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

It worked fine with my *.fnt file. 我的*.fnt文件运行正常。 Maybe your *.fnt filepath is wrong. 也许您的*.fnt路径错误。 Normally with that path it should be located at android/assets/skinTest.fnt . 通常,该路径应位于android/assets/skinTest.fnt If you are testing the application on desktop you should also set you working directory to the assets folder. 如果要在桌面上测试应用程序,则还应该将工作目录设置为assets文件夹。 (I'm using Android Studio) (我正在使用Android Studio)

Looking at the sample skin on https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests-android/assets/data/uiskin.json https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests-android/assets/data/uiskin.json上查看示例皮肤

I noticed that there are no quotations around the style classes, properties and values. 我注意到样式类,属性和值周围没有引号。

Perhaps changing your file to something like the following would work: 也许可以将文件更改为以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM