LibgGdx - 在AssetManager中加载TrueTypeFont

时间:2015-09-23 22:53:48

标签: java fonts libgdx

我正在尝试在我的LibGdx .ttf中加载AssetManager字体,但我似乎无法将其弄好。我尝试了什么:

在我的AssetManager类中:

public static void load(){
//...
//Fonts
FileHandleResolver resolver = new InternalFileHandleResolver();
manager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
manager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
manager.load(fontTest, FreeTypeFontGenerator.class); //fontTest is a ttf-font

然后我尝试在我的屏幕中使用它:

FreeTypeFontGenerator generator = GdxAssetManager.manager.get(GdxAssetManager.fontTest, FreeTypeFontGenerator.class);
params.size = 50;
font = generator.generateFont(params); //set the bitmapfont to the generator with parameters

这给了我很多奇怪的错误。我甚至不知道在哪里寻找故障。有谁知道如何做到这一点?

1 个答案:

答案 0 :(得分:3)

是的,因为他们没有加载波音。它是FreeTypeFontGeneratorLoader的一种错误或误导性设计。这是您需要这样做才能使其工作的方式:

// set the loaders for the generator and the fonts themselves
FileHandleResolver resolver = new InternalFileHandleResolver();
manager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
manager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));

// load to fonts via the generator (implicitely done by the FreetypeFontLoader).
// Note: you MUST specify a FreetypeFontGenerator defining the ttf font file name and the size
// of the font to be generated. 
FreeTypeFontLoaderParameter size1Params = new FreeTypeFontLoaderParameter();            
size1Params.fontFileName = "ls-bold.otf";//name of file on disk
size1Params.fontParameters.size = ((int)((Gdx.graphics.getWidth()*0.10f)));         
manager.load("fontWinFail.ttf", BitmapFont.class, size1Params);//BUGGY:We need to append .ttf otherwise wont work...just put any name here and append .ttf MANDATORY(this is the trick)

查看最后一行,您必须将.ttf附加到您选择的任何随机名称。这就是诀窍。

现在,您想要评论的示例是这样的:

BitmapFont                      fontWinFail; 
fontWinFail = manager.get("fontWinFail.ttf", BitmapFont.class);//notice same name used in above segment(NOTE that this is just a name, not the name of the file)         
fontWinFail.setColor(Color.BLACK);

//Then on your render() method
fontWinFail.draw(batch, "hello world", Gdx.graphics.getWidth()*0.5f, Gdx.graphics.getHeight()*0.6f);