对于libgdx表JSON文件文件、libgdx、JSON

2023-09-06 07:29:25 作者:逗比小伙!

我试图让具有皮肤基于libgdx的默认 LabelStyle 和 BitmapFont 通过使用 JSON 文件。 (该文件应该持有这两个对象的引用),在这一过程中,我得到一些不明错误。例如:

I am trying to make a Table that has a Skin based on libgdx's default LabelStyle and BitmapFont through the use of a json file. (this file is supposed to hold the references of these two objects) In doing this I am getting some unclear errors. For instance:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.SerializationException: Error reading file: ui/uiskin.json
        at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:96)
        at com.badlogic.gdx.scenes.scene2d.ui.Skin.<init>(Skin.java:73)
        at com.naitsirc.Interpolation.Test.show(Test.java:47)
        at com.badlogic.gdx.Game.setScreen(Game.java:61)
        at com.naitsirc.Interpolation.InterpolationTest.create(InterpolationTest.java:9)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
    Caused by: com.badlogic.gdx.utils.SerializationException: Error reading file: ui/uiskin.json
        at com.badlogic.gdx.utils.Json.fromJson(Json.java:662)
        at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:94)
        ... 6 more
    Caused by: com.badlogic.gdx.utils.SerializationException: Serialization trace:
    font (com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle)
        at com.badlogic.gdx.utils.Json.readFields(Json.java:762)
        at com.badlogic.gdx.utils.Json.readValue(Json.java:865)
        at com.badlogic.gdx.scenes.scene2d.ui.Skin$1.readValue(Skin.java:418)
        at com.badlogic.gdx.utils.Json.readValue(Json.java:809)
        at com.badlogic.gdx.scenes.scene2d.ui.Skin$2.readNamedObjects(Skin.java:439)
        at com.badlogic.gdx.scenes.scene2d.ui.Skin$2.read(Skin.java:428)
        at com.badlogic.gdx.scenes.scene2d.ui.Skin$2.read(Skin.java:424)
        at com.badlogic.gdx.utils.Json.readValue(Json.java:839)
        at com.badlogic.gdx.scenes.scene2d.ui.Skin$1.readValue(Skin.java:418)
        at com.badlogic.gdx.utils.Json.fromJson(Json.java:660)
        ... 7 more
    Caused by: com.badlogic.gdx.utils.GdxRuntimeException: No com.badlogic.gdx.graphics.g2d.BitmapFont registered with name: default-font
        at com.badlogic.gdx.scenes.scene2d.ui.Skin.get(Skin.java:145)
        at com.badlogic.gdx.scenes.scene2d.ui.Skin$1.readValue(Skin.java:417)
        at com.badlogic.gdx.utils.Json.readFields(Json.java:755)
        ... 16 more*

这是我创造我的皮肤 JSON

public void show() {
    Gdx.input.setInputProcessor(stage = new Stage());

    skin = new Skin(Gdx.files.internal("ui/uiskin.json")); // HERE

    container = new Table();
    table = new Table(skin);

    for(int i = 0; i < interpolationNames.length; i++){
        //table.row();
        table.add(interpolationNames[i]); // I am trying to populate these table cells
    }

    ScrollPane pane = new ScrollPane(table);
    container.add(pane);        

    stage.addActor(container);

    renderer = new ShapeRenderer();
}

我的 JSON 文件:

{
    com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle: {
        default: { font: default-font, fontColor: white }
    },  
    com.badlogic.gdx.graphics.g2d.BitmapFont: {
        default-font: { file: default.fnt }
    }
}

我在做什么错误?

What am I doing incorrectly?

我似乎有同样的问题,因为这里 但我所做的是同样的事情,它不工作。

I seem to have the same problem as here but what I did is the same thing, and its not working.

推荐答案

现在的问题是,在我的 JSON 文件对象的顺序,我也错过了一些需要的对象为的ScrollPane 颜色。此外,我需要 default.fnt 为Default.png 添加到我的资产文件夹从这里 。基本上,它应该是这样的:

The problem is with the order of the objects in my json file and also I missed some required objects for the ScrollPane and Color. In addition I needed to add default.fnt and default.png to my assets folder from here. Basically its supposed to look like this:

{
    com.badlogic.gdx.graphics.g2d.BitmapFont: { 
        default-font: { file: default.fnt } 
    },
    com.badlogic.gdx.graphics.Color: {
        white: { a: 1, b: 1, g: 1, r: 1 },
    },
    com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle: {
        default: { font: default-font, fontColor: white }
    },
    com.badlogic.gdx.scenes.scene2d.ui.ScrollPane$ScrollPaneStyle: {
        default: { vScroll: default-scroll, hScrollKnob: default-round-large, background: default-rect, hScroll: default-scroll, vScrollKnob: default-round-large }
    }
}

这说我还是不明白为什么需要一定的顺序也不知道如何找出哪些特定对象需要放在 JSON 文件。唯一的办法我想通了,我需要这些专门是Eclipse的给我的错误。

This said I still don't understand why a certain order is required nor do I know how to find out which specific objects need to be placed in the json file. The only way I figured out that I need these specifically is by the errors Eclipse gave me.