LibGdx - 从资产管理器加载json文件时出错

时间:2016-05-17 22:17:44

标签: json libgdx

我正在使用assetmanager加载皮肤,并因<table class="table table-bordered"> <?php foreach ($mag->records as $record) : ?> <tr><th style='text-align:center'>ID</th><th style='text-align:center'>DODAŁ</th><th style='text-align:center'>NAZWA</th><th style='text-align:center'>KATEGORIA</th><th style='text-align:center'>SERIAL</th><th style='text-align:center'>ILOŚĆ</th><th style='text-align:center'>DATA</th><th style='text-align:center'>GODZINA</th><th style='text-align:center' colspan='2'>AKCJA</th></tr> <?php $color = NULL; $color1 = '#99bbff'; $color2 = '#b3ccff'; $color == $color1 ? $color = $color2 : $color = $color1; echo "<tr class='active'>"; echo "<td align='center' style='background-color:" . $color . ";'>" . $record['id'] . "</td>"; echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=userpanel&user=" . $record['added'] . "'>" . $record['added'] . "</a></td>"; echo "<td align='center' style='background-color:" . $color . ";'>" . $record['nazwa'] . "</td>"; echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=category&cat=" . $record['kategoria'] . "'>" . $record['kategoria'] . "</a></td>"; echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=editserial&sn=" . $record['sn'] . "'>" . $record['sn'] . "</a></td>"; echo "<td align='center' style='background-color:" . $color . ";'>" . $record['quantity'] . "</td>"; echo "<td align='center' style='background-color:" . $color . ";'>" . $record['data'] . "</td>"; echo "<td align='center' style='background-color:" . $color . ";'>" . $record['godzina'] . "</td>"; echo "<td align='center'><a href='panel.php?page=magazyn&action=edit&sn=" . $record['sn'] . "&nazwa=" . $record['nazwa'] . "&kategoria=" . $record['kategoria'] . "&id=" . $record['id'] . "'</a><button class='btn btn-default'>Edytuj</button></td>"; echo "<td align='center'><a href='panel.php?page=magazyn&action=delete&sn=" . $record['sn'] . "'</a><button class='btn btn-default'>Usuń</button></td>"; echo "</tr>"; endforeach; ?> </table> 而不断收到错误。我试图在我的json文件中使用此字体来创建Label样式。 我在皮肤参数中添加了字体和地图集,我在这里遗漏了什么吗?

我像这样加载我的皮肤:

There is no Bitmapfont registered with name: font_dpb_59

如您所见,我将字体添加为参数,但出现错误。不知道我做错了什么,字体是在皮肤之前加载的。

完整堆栈跟踪:

        ObjectMap<String, Object> font_map = new ObjectMap<String, Object>();
        font_map.put("font_dpb_59", font_dpb_59);
        font_map.put("font_dpm_40", font_dpm_40);
        //... some more fonts

        manager.load(skinAtlas, TextureAtlas.class);
        manager.load(menuSkin, Skin.class, new SkinLoader.SkinParameter(skinAtlas, font_map));

如果这有帮助,这是我的json文件:

    Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.SerializationException: Error reading file: menu_ui/ui_skin.json
    at com.badlogic.gdx.assets.AssetManager.handleTaskError(AssetManager.java:570)
    at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:375)
    at com.scene2d.space_camp.MyGdxGame.create(MyGdxGame.java:127)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:147)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
Caused by: com.badlogic.gdx.utils.SerializationException: Error reading file: menu_ui/ui_skin.json
    at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:97)
    at com.badlogic.gdx.assets.loaders.SkinLoader.loadSync(SkinLoader.java:75)
    at com.badlogic.gdx.assets.loaders.SkinLoader.loadSync(SkinLoader.java:38)
    at com.badlogic.gdx.assets.AssetLoadingTask.handleAsyncLoader(AssetLoadingTask.java:141)
    at com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:90)
    at com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:498)
    at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:373)
    ... 3 more
Caused by: com.badlogic.gdx.utils.SerializationException: Error reading file: menu_ui/ui_skin.json
    at com.badlogic.gdx.utils.Json.fromJson(Json.java:694)
    at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:95)
    ... 9 more
Caused by: com.badlogic.gdx.utils.SerializationException: Serialization trace:
{}."com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle".font_dpb_59.font
font (com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle)
    at com.badlogic.gdx.utils.Json.readFields(Json.java:804)
    at com.badlogic.gdx.utils.Json.readValue(Json.java:919)
    at com.badlogic.gdx.scenes.scene2d.ui.Skin$1.readValue(Skin.java:408)
    at com.badlogic.gdx.utils.Json.readValue(Json.java:852)
    at com.badlogic.gdx.scenes.scene2d.ui.Skin$2.readNamedObjects(Skin.java:429)
    at com.badlogic.gdx.scenes.scene2d.ui.Skin$2.read(Skin.java:418)
    at com.badlogic.gdx.scenes.scene2d.ui.Skin$2.read(Skin.java:414)
    at com.badlogic.gdx.utils.Json.readValue(Json.java:884)
    at com.badlogic.gdx.scenes.scene2d.ui.Skin$1.readValue(Skin.java:408)
    at com.badlogic.gdx.utils.Json.fromJson(Json.java:692)
    ... 10 more
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: No com.badlogic.gdx.graphics.g2d.BitmapFont registered with name: font_dpb_59
    at com.badlogic.gdx.scenes.scene2d.ui.Skin.get(Skin.java:149)
    at com.badlogic.gdx.scenes.scene2d.ui.Skin$1.readValue(Skin.java:407)
    at com.badlogic.gdx.utils.Json.readFields(Json.java:797)
    ... 19 more

编辑:

这是我加载字体的方式:

{
  "com.badlogic.gdx.graphics.Color": {
    "white": {
      "r": 1, "g": 1, "b": 1, "a": 1
    }
  },

  "com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle": {
    "font_dpb_59": {
      "font": "font_dpb_59", "fontColor": "white"
    }
  },

  "com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle": {
    "txt_btn_white": {
      "up": "button1",
      "down": "button1",
      "font": "font_dpb_59",
      "fontColor": "white",
      "pressedOffsetX": 1,
      "pressedOffsetY": -1 }
  },

  "com.badlogic.gdx.scenes.scene2d.ui.Button$ButtonStyle": {
    "btn_white": {
      "up": "button1" ,
      "down": "button1",
      "pressedOffsetX": 1,
      "pressedOffsetY": -1 }
  },
  "com.badlogic.gdx.scenes.scene2d.ui.ScrollPane$ScrollPaneStyle": {
    "default": { }
  },

  "com.badlogic.gdx.scenes.scene2d.ui.Window$WindowStyle": {
    "default": { "titleFont": "font_dpb_59", "titleFontColor": "white" }
  }
}

1 个答案:

答案 0 :(得分:1)

您在此处传递到ObjectMap的对象:

ObjectMap<String, Object> font_map = new ObjectMap<String, Object>();
font_map.put("font_dpb_59", font_dpb_59);
font_map.put("font_dpm_40", font_dpm_40);

必须是BitmapFont对象,而不是String路径。要拥有BitmapFont实例,您必须已经完成加载一个实例。如果您使用AssetManager加载它们,则必须在资产管理器上调用finishLoading,然后调用get(".....", BitmapFont.class)以获取要放入对象图中的BitmapFont引用。

我原以为你这样做是因为你在运行时使用FreeTypeFontGenerator创建了BitmapFonts,所以无法从Json加载它们。但是,如果您已经提前获得了字体文件,那么您无法直接将它们放入皮肤的Json文件中:

com.badlogic.gdx.graphics.g2d.BitmapFont: {
    font_dpb_59: { file: path/to/font_dpb_59.fnt,
                   scaledSize: 12, //integer height of capital letters
                   markupEnabled: false,
                   flip : false},
}