我正在尝试在LibGdx AssetManager中加载.ttf字体,但似乎无法正确处理.我试过的
在我的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
这给了我很多奇怪的错误.我什至不知道在哪里寻找缺点.有谁知道如何做到这一点?
解决方法:
是的,这是因为它们没有装满波音. 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);