以编程方式更改字体时,Libgdx外观未更新 [英] Libgdx Skin not Updating when Changing Font Programmatically

查看:122
本文介绍了以编程方式更改字体时,Libgdx外观未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我将皮肤作为json文件加载.在文件中,我放置了两个BitmapFont定义(字体"和大字体"),并将它们实际上留为空白(我确实加载了一个虚拟的".fnt"文件以防止解析器抱怨).然后,我使用以下代码生成了两种FreeType字体:

First, I loaded the skin as a json file. In the file, I put two BitmapFont defintions ("font" and "big-font") and left them practically blank (I did load a dummy ".fnt" file to prevent the parser from complaining). I then generated two FreeType Fonts using the following code:

//start font generator
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/button-font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameters = new FreeTypeFontGenerator.FreeTypeFontParameter();

//default font (the sizes are arbitrary, since it did not work...)
parameters.size = 10;
BitmapFont defaultFont = generator.generateFont(parameters);

//big font
parameters.size = 100;
BitmapFont bigFont = generator.generateFont(parameters);

并将它们设置为皮肤字体,如下所示:

And set them as the skin fonts like so:

skin.add("font", defaultFont, BitmapFont.class);
skin.add("big-font", bigFont, BitmapFont.class);

问题在于gui(尤其是使用"font"字体的TextButton)似乎并不关心我以编程方式所做的事情,而是继续使用在json文件中设置的虚拟字体.

The problem is that the gui (particularly the TextButton, which uses the "font" font) does not seem to care about what I did programmatically and keeps using the dummy font that I've set in the json file.

我做错了什么?有没有更好的方法来集成ttf字体和json皮肤解析?

What am I doing wrong? Is there a better way to integrate ttf fonts and json skin parsing?

在此先感谢您的帮助.

推荐答案

请按照以下步骤操作:

  1. 首先,从TTF文件创建字体

  1. First, create your font from a TTF file

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/button-font.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 10;
BitmapFont defaultFont = generator.generateFont(parameter); 
generator.dispose(); // don't forget to dispose to avoid memory leaks!

  • 使用默认构造函数创建空白外观并将字体添加到外观中.

  • Create a blank skin using default constructor and add your font to the skin.

    Skin skin = new Skin();
    skin.add("font",defaultFont);
    

  • 将地图集文件添加到皮肤.

  • Add your atlas file to your skin.

    skin.addRegion(new TextureAtlas(Gdx.files.internal("mySkin.atlas")));
    

  • 将您的Json文件加载到皮肤上.

  • Load your Json file to your skin.

    skin.load(Gdx.files.internal("mySkin.json"));
    

  • 然后,在您的JSON文件中,您可以引用字体"

  • then, in your JSON file you can reference "font"

    {
      font: font
    }
    

  • 这篇关于以编程方式更改字体时,Libgdx外观未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆