代码之家  ›  专栏  ›  技术社区  ›  Manh Khôi Duong

将TTF字体加载到AssetManager时出现异常

  •  0
  • Manh Khôi Duong  · 技术社区  · 7 年前

    https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/extensions/FreeTypeFontLoaderTest.java

    使用不同的参数和文件名将一个ttf文件放入AssetManager .

    public class LoadingScreen implements Screen {
    
        private final GdxAsternets game;
    
        private float progress;
    
        public LoadingScreen(final GdxAsternets game){
            this.game = game;
    
            game.bitmapFont = new BitmapFont(Gdx.files.internal("font/myFont.fnt"));
    
            queueAssets();
        }
    
        @Override
        public void show() {
            progress = 0f;
        }
    
        @Override
        public void render(float delta) {
            Gdx.gl.glClearColor(0, 0, 0, 0);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    
            update(delta);
        }
    
        private void update(float delta){
            progress = MathUtils.lerp(progress, game.assetManager.getProgress(), 0.1f);
            if(game.assetManager.update() && progress >= game.assetManager.getProgress()- 0.1f){
                game.setScreen(game.menuScreen);
            }
        }
    
        private void queueAssets(){
            //Fontloader/Filehandler
            FileHandleResolver resolver = new InternalFileHandleResolver();
            game.assetManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
            game.assetManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
    
            //load timer font
            FreetypeFontLoader.FreeTypeFontLoaderParameter smallFontParams = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
            smallFontParams.fontFileName = "font/Montserrat-SemiBold.ttf";
            smallFontParams.fontParameters.size = 40;
            game.assetManager.load("smallFont.ttf", BitmapFont.class, smallFontParams);
    
            //load game msg font
            FreetypeFontLoader.FreeTypeFontLoaderParameter bigFontParams = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
            smallFontParams.fontFileName = "font/Montserrat-SemiBold.ttf";
            smallFontParams.fontParameters.size = 85;
            game.assetManager.load("bigFont.ttf", BitmapFont.class, bigFontParams);
        }
        /**Overring methods...*/
    }
    

    查看方法 . 引用我上面的链接: “字体名称是任意的,不指向文件盘” 我利用了

    game.assetManager.load("font/Montserrat-SemiBold.ttf", BitmapFont.class, smallFontParams);
    

    game.assetManager.load("smallFont.ttf", BitmapFont.class, smallFontParams);
    

    更新(浮点增量)方法引发异常。资产管理人。正在调用update() .

    例外情况:

    GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: null (Internal)
    at com.bitbench.asternets.Screen.LoadingScreen.update(LoadingScreen.java:84)
    at com.bitbench.asternets.Screen.LoadingScreen.render(LoadingScreen.java:67)
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Manh Khôi Duong    7 年前

    我花了几天时间才解决这个问题,但对于同样面临同样问题的人来说,以下是答案:

        FreetypeFontLoader.FreeTypeFontLoaderParameter smallFontParams = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
        smallFontParams.fontFileName = "font/Montserrat-SemiBold.ttf";
        smallFontParams.fontParameters.size = 40;
        game.assetManager.load("smallFont.ttf", BitmapFont.class, smallFontParams);
    
        FreetypeFontLoader.FreeTypeFontLoaderParameter bigFontParams = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
        smallFontParams.fontFileName = "font/Montserrat-SemiBold.ttf";
        smallFontParams.fontParameters.size = 85;
        game.assetManager.load("bigFont.ttf", BitmapFont.class, bigFontParams);
    

    使用相同的FreeTypeFontLoaderParameter并更改其参数以创建字体将导致出现异常。因此,使用不同的FreeTypeFontLoaderParameter来创建具有不同参数的字体!正确的代码应为:

        FreetypeFontLoader.FreeTypeFontLoaderParameter smallFontParams = new 
        FreetypeFontLoader.FreeTypeFontLoaderParameter();
        smallFontParams.fontFileName = "font/Montserrat-SemiBold.ttf";
        smallFontParams.fontParameters.size = 40;
        game.assetManager.load("smallFont.ttf", BitmapFont.class, smallFontParams);
    
        FreetypeFontLoader.FreeTypeFontLoaderParameter bigFontParams = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
        bigFontParams.fontFileName = "font/Montserrat-SemiBold.ttf";
        bigFontParams.fontParameters.size = 85;
        game.assetManager.load("bigFont.ttf", BitmapFont.class, bigFontParams);