libgdx - 解析default.fnt时读取uiskin.json,NumberFormatException时出错

时间:2015-12-22 05:38:27

标签: java libgdx numberformatexception

我在为libgdx实现默认用户界面外观时遇到问题。当我尝试将uiskin.json作为纹理图集加载时,在尝试解析包含NumberFormatException的行时,会以错误的方式讨论default.fnt

这是我的堆栈跟踪错误。

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Error reading pack file: uiskin.json
    at com.badlogic.gdx.assets.AssetManager.handleTaskError(AssetManager.java:560)
    at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:365)
    at com.dizydev.robotwars.screens.LoadScreen.update(LoadScreen.java:64)
    at com.dizydev.robotwars.screens.AbstractScreen.render(AbstractScreen.java:55)
    at com.dizydev.robotwars.RobotWars.render(RobotWars.java:41)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Error reading pack file: uiskin.json
    at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:187)
    at com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:58)
    at com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:34)
    at com.badlogic.gdx.assets.AssetLoadingTask.handleSyncLoader(AssetLoadingTask.java:99)
    at com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:88)
    at com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:488)
    at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:363)
    ... 5 more
Caused by: java.lang.NumberFormatException: For input string: "{ default-font: { file: default.fnt } }"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:116)
    ... 11 more

以下是我用来加载默认用户界面皮肤的代码。

game.manager.load("uiskin.json", TextureAtlas.class);
skin = new Skin(game.manager.get("uiskin.json", TextureAtlas.class));

我的assets文件夹中包含所有必需文件,这里是文件列表。它们都在默认的assets文件夹中。

default.fnt
default.png
uiskin.atlas
uiskin.json
uiskin.png

因为看起来错误是在uiskin.json文件的解析中,所以这里是我拥有的uiskin.json文件的副本。

{
com.badlogic.gdx.graphics.g2d.BitmapFont: { default-font: { file: default.fnt } },
com.badlogic.gdx.graphics.Color: {
    green: { a: 1, b: 0, g: 1, r: 0 },
    white: { a: 1, b: 1, g: 1, r: 1 },
    red: { a: 1, b: 0, g: 0, r: 1 },
    black: { a: 1, b: 0, g: 0, r: 0 },
},
com.badlogic.gdx.scenes.scene2d.ui.Skin$TintedDrawable: {
    dialogDim: { name: white, color: { r: 0, g: 0, b: 0, a: 0.45 } },
},
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 }
},
com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
    default: { down: default-round-down, up: default-round, font: default-font, fontColor: white },
    toggle: { down: default-round-down, up: default-round, checked: default-round-down, font: default-font, fontColor: white, downFontColor: red }
},
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 }
},
com.badlogic.gdx.scenes.scene2d.ui.SelectBox$SelectBoxStyle: {
    default: {
        font: default-font, fontColor: white, background: default-select,
        scrollStyle: default,
        listStyle: { font: default-font, selection: default-select-selection }
    }
},
com.badlogic.gdx.scenes.scene2d.ui.SplitPane$SplitPaneStyle: {
    default-vertical: { handle: default-splitpane-vertical },
    default-horizontal: { handle: default-splitpane }
},
com.badlogic.gdx.scenes.scene2d.ui.Window$WindowStyle: {
    default: { titleFont: default-font, background: default-window, titleFontColor: white },
    dialog: { titleFont: default-font, background: default-window, titleFontColor: white, stageBackground: dialogDim }
},
com.badlogic.gdx.scenes.scene2d.ui.ProgressBar$ProgressBarStyle: {
    default-horizontal: { background: default-slider, knob: default-slider-knob },
    default-vertical: { background: default-slider, knob: default-round-large }
},
com.badlogic.gdx.scenes.scene2d.ui.Slider$SliderStyle: {
    default-horizontal: { background: default-slider, knob: default-slider-knob },
    default-vertical: { background: default-slider, knob: default-round-large }
},
com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle: {
    default: { font: default-font, fontColor: white }
},
com.badlogic.gdx.scenes.scene2d.ui.TextField$TextFieldStyle: {
    default: { selection: selection, background: textfield, font: default-font, fontColor: white, cursor: cursor }
},
com.badlogic.gdx.scenes.scene2d.ui.CheckBox$CheckBoxStyle: {
    default: { checkboxOn: check-on, checkboxOff: check-off, font: default-font, fontColor: white }
},
com.badlogic.gdx.scenes.scene2d.ui.List$ListStyle: {
    default: { fontColorUnselected: white, selection: selection, fontColorSelected: white, font: default-font }
},
com.badlogic.gdx.scenes.scene2d.ui.Touchpad$TouchpadStyle: {
    default: { background: default-pane, knob: default-round-large }
},
com.badlogic.gdx.scenes.scene2d.ui.Tree$TreeStyle: {
    default: { minus: tree-minus, plus: tree-plus, selection: default-select-selection }
},
com.badlogic.gdx.scenes.scene2d.ui.TextTooltip$TextTooltipStyle: {
    default: {
        label: { font: default-font, fontColor: white },
        background: default-pane, wrapWidth: 150
    }
},
}

有谁知道我收到此错误的原因?它对我来说似乎并不直观,而且我不确定它是否是解析错误或其他因为默认用户界面皮肤所需的所有文件都是可用的最新版本。如果有人能帮助我,我将不胜感激。

编辑:

给我的第一个答案并没有解决我的问题。以下是我为尝试第一个解决方案所做的代码更改。

game.manager.load("uiskin.png", TextureAtlas.class);
skin = new Skin(Gdx.files.internal("uiskin.json"), game.manager.get("uiskin.png", TextureAtlas.class));

这是它为我提供的错误堆栈跟踪。

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Error reading pack file: uiskin.png
    at com.badlogic.gdx.assets.AssetManager.handleTaskError(AssetManager.java:560)
    at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:365)
    at com.dizydev.robotwars.screens.LoadScreen.update(LoadScreen.java:64)
    at com.dizydev.robotwars.screens.AbstractScreen.render(AbstractScreen.java:55)
    at com.dizydev.robotwars.RobotWars.render(RobotWars.java:41)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Error reading pack file: uiskin.png
    at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:187)
    at com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:58)
    at com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:34)
    at com.badlogic.gdx.assets.AssetLoadingTask.handleSyncLoader(AssetLoadingTask.java:99)
    at com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:88)
    at com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:488)
    at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:363)
    ... 5 more
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Invalid line: 
    at com.badlogic.gdx.graphics.g2d.TextureAtlas.readTuple(TextureAtlas.java:443)
    at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:115)
    ... 11 more

编辑:

在下面的帮助下,我能够找出问题所在。我不得不从uiskin.json文件中删除一个设置,因为它没有被正确解析。

我不得不改变这一点。

com.badlogic.gdx.scenes.scene2d.ui.TextTooltip$TextTooltipStyle: {
    default: {
        label: { font: default-font, fontColor: white },
        background: default-pane, wrapWidth: 150
    }
},

对此。

com.badlogic.gdx.scenes.scene2d.ui.TextTooltip$TextTooltipStyle: {
    default: {
        label: { font: default-font, fontColor: white },
        background: default-pane
    }
},

我的新代码看起来像这样。

game.manager.load("uiskin.json", Skin.class);
skin = game.manager.get("uiskin.json", Skin.class);

1 个答案:

答案 0 :(得分:1)

libgdx引擎已打开,您可以在github中查看源代码,因此have a look at place of your error通常是个好主意。如您所见,问题在于行

    116.    width = Integer.parseInt(tuple[0]);      // <- this especially
    117.    height = Integer.parseInt(tuple[1]);

我们现在知道的是错误与创建TextureAtlas实例有关。

看看你将textureAtlas加载到AssetManager的方式(因为我认为 game.manager 是AssetManager)。你应该使用

    game.manager.load("uiskin.atlas", TextureAtlas.class);

而不是

    game.manager.load("uiskin.json", TextureAtlas.class);

.json文件不是atlas定义,您需要创建TextureAtlas实例。 Atlas定义包含atlas图形上图像的位置和大小。

然后,加载TextureAtlas,你应该使用Skin的构造函数

    Skin(FileHandle skinFile, TextureAtlas atlas)

    skin = new Skin(Gdx.files.internal("uiskin.json"), game.manager.get("uiskin.png", TextureAtlas.class));

您正在为Skin 提供JSON定义,然后为它提供TextureAtlas。

这是你应该这样做的方式。