Libgdx Skin not Updating when Changing Font Programmatically

无人久伴 提交于 2020-01-07 04:30:40

问题


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);

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.

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

Thanks in advance for any help.


回答1:


Follow these steps :

  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!
    
  2. Create a blank skin using default constructor and add your font to the skin.

    Skin skin = new Skin();
    skin.add("font",defaultFont);
    
  3. Add your atlas file to your skin.

    skin.addRegion(new TextureAtlas(Gdx.files.internal("mySkin.atlas")));
    
  4. Load your Json file to your skin.

    skin.load(Gdx.files.internal("mySkin.json"));
    
  5. then, in your JSON file you can reference “font”

    {
      font: font
    }
    


来源:https://stackoverflow.com/questions/45523878/libgdx-skin-not-updating-when-changing-font-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!