读取文件skin.json libgdx时出错

时间:2016-02-16 12:48:49

标签: java json eclipse libgdx

我正在尝试为我的朋友和我正在尝试制作的游戏创建一个主菜单。我们使用Eclipse + LibGdx,我真的不知道我在做什么。我直接使用代码,我无法在任何博客/论坛/教程中找到任何帮助。 (http://www.gamefromscratch.com/post/2015/02/03/LibGDX-Video-Tutorial-Scene2D-UI-Widgets-Layout-and-Skins

    package com.mygdx.game;
    import com.badlogic.gdx.ApplicationAdapter;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.graphics.GL20;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.g2d.SpriteBatch;
    import com.badlogic.gdx.scenes.scene2d.InputEvent;
    import com.badlogic.gdx.scenes.scene2d.Stage;
    import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
    import com.badlogic.gdx.scenes.scene2d.ui.Skin;
    import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
    import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
    import com.badlogic.gdx.utils.Timer; 
    import com.badlogic.gdx.utils.viewport.ScreenViewport;

    public class MyGdxGame extends ApplicationAdapter {
    private Skin skin;  
    private Stage stage;

    @Override
    public void create () {

    skin = new Skin(Gdx.files.internal("uiskin.json"));
    stage = new Stage(new ScreenViewport());

    final TextButton button = new TextButton("Click Me",skin,"default");
    button.setWidth(200);
    button.setHeight(50);

    final Dialog dialog = new Dialog("Click Message",skin);

    button.addListener(new ClickListener(){
       @Override
       public void clicked(InputEvent event, float x, float y) {
          dialog.show(stage);
          Timer.schedule(new Timer.Task() {
             @Override
             public void run() {
                dialog.hide();
             }
          }, 10);
       }
    });
    stage.addActor(button);

    Gdx.input.setInputProcessor(stage);
 }

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

 }

     public static void main(String[] args)
     {;}
}

uiskin.json文件:

    {
     com.badlogic.gdx.graphics.g2d.BitmapFont: { default-font: { file: Razer.fnt     } },
      com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
      default: { down: default-round-down, up: default-round, font: default-font     },
      },

    com.badlogic.gdx.scenes.scene2d.ui.Window$WindowStyle: {
    default:  {
    titleFont: default-font
         }
       }
    }

desktoplauncher:

    package com.mygdx.game.desktop;

    import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
    import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
    import com.mygdx.game.MyGdxGame;

    public class DesktopLauncher {
        public static void main (String[] arg) {
            LwjglApplicationConfiguration config = new       LwjglApplicationConfiguration();
        new LwjglApplication(new MyGdxGame(), config);
    }
}

错误:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.SerializationException: Error reading file: uiskin.json
    at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:97)
    at com.badlogic.gdx.scenes.scene2d.ui.Skin.(Skin.java:74)
    at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:26)
    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: uiskin.json
    at com.badlogic.gdx.utils.Json.fromJson(Json.java:694)
    at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:95)
    ... 4 more
Caused by: com.badlogic.gdx.utils.SerializationException: Font file not found: Razer.fnt
    at com.badlogic.gdx.scenes.scene2d.ui.Skin$3.read(Skin.java:452)
    at com.badlogic.gdx.scenes.scene2d.ui.Skin$3.read(Skin.java:443)
    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.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)
    ... 5 more

感谢。在我的脑海里,我完全迷失了,我是最后的选择。对于我在这篇文章中犯的任何错误感到抱歉!!

-ed

2 个答案:

答案 0 :(得分:2)

该错误基本上是说皮肤json有问题。

格式虽然看起来很好,但我认为堆栈跟踪中还有更多。请查看Razer.fnt的路径。

如果没有解决问题,请在此处粘贴完整的堆栈跟踪。

答案 1 :(得分:0)

Font file not found: Razer.fnt

您必须使用Hiero tool来创建字体。将android项目中生成的两个文件放在Assets文件夹中。如果您没有" Razer字体",请搜索某些字体网站