使用libgdx crash

时间:2016-01-12 14:48:28

标签: android-studio fonts libgdx load

我有加载ttf文件的问题,我的代码:

Label migliaLabel;
migliaLabel = new Label("label", new Label.LabelStyle(new BitmapFont(Gdx.files.internal("Kalam-Regular.ttf")), Color.MAGENTA));

文件Kalam-Regular.ttf位于assets / Kalam-Regular.ttf文件夹中 但是当我运行游戏时,android studio出错:

  

致命异常:GLThread 125                                                                                  com.badlogic.gdx.utils.GdxRuntimeException:加载字体文件时出错:   卡拉姆 - Regular.ttf                                                                                      在   com.badlogic.gdx.graphics.g2d.BitmapFont $ BitmapFontData.load(BitmapFont.java:665)                                                                                      在   com.badlogic.gdx.graphics.g2d.BitmapFont $ BitmapFontData。(BitmapFont.java:475)                                                                                      在   com.badlogic.gdx.graphics.g2d.BitmapFont。(BitmapFont.java:114)                                                                                      在   com.badlogic.gdx.graphics.g2d.BitmapFont。(BitmapFont.java:107)                                                                                      在com.surfsurvivor.game.GameClass.show(GameClass.java:181)                                                                                      在com.badlogic.gdx.Game.setScreen(Game.java:61)                                                                                      在com.surfsurvivor.game.SurfClass.create(SurfClass.java:26)                                                                                      在   com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:254)                                                                                      在   android.opengl.GLSurfaceView $ GLThread.guardedRun(GLSurfaceView.java:1505)                                                                                      在android.opengl.GLSurfaceView $ GLThread.run(GLSurfaceView.java:1240)                                                                                   引起:com.badlogic.gdx.utils.GdxRuntimeException:无效   填充。                                                                                      在   com.badlogic.gdx.graphics.g2d.BitmapFont $ BitmapFontData.load(BitmapFont.java:488)                                                                                      在   com.badlogic.gdx.graphics.g2d.BitmapFont $ BitmapFontData。(BitmapFont.java:475)                                                                                      在   com.badlogic.gdx.graphics.g2d.BitmapFont。(BitmapFont.java:114)                                                                                      在   com.badlogic.gdx.graphics.g2d.BitmapFont。(BitmapFont.java:107)                                                                                      在com.surfsurvivor.game.GameClass.show(GameClass.java:181)                                                                                      在com.badlogic.gdx.Game.setScreen(Game.java:61)                                                                                      在com.surfsurvivor.game.SurfClass.create(SurfClass.java:26)                                                                                      在   com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:254)                                                                                      在   android.opengl.GLSurfaceView $ GLThread.guardedRun(GLSurfaceView.java:1505)                                                                                      在android.opengl.GLSurfaceView $ GLThread.run(GLSurfaceView.java:1240)

我该如何解决?

2 个答案:

答案 0 :(得分:0)

我使用Hiero解决了,您可以从网站libgdx下载并转换文件ttf文件.fnt。现在它对我有用。

现在设置字体:

migliaLabel = new Label("label", new Label.LabelStyle(new BitmapFont(Gdx.files.internal("Kalam-Regular.fnt")), Color.MAGENTA));

答案 1 :(得分:0)

在处理TTF文件时,LibGDX有一个名为FreeType的库,可让您从TTF文件生成BitmapFont对象。如何从TTF文件生成字体的示例可能如下所示:

    /**Initialises the generator using the file location given.*/
    generator = new FreeTypeFontGenerator(Gdx.files.local(fontLocation));
    params = new FreeTypeFontParameter();

    /**Sets the parameters of the object constant for the font, regardless of size.*/
    params.borderWidth = BORDER_WIDTH;
    params.borderColor = Color.BLACK;
    params.characters = FreeTypeFontGenerator.DEFAULT_CHARS;
    params.magFilter = TextureFilter.Nearest;
    params.minFilter = TextureFilter.Nearest;
    params.genMipMaps = true;
    params.size = FONT_SIZE;

    /**Generates the font using the generator object.*/
    font = generator.generateFont(params);