2015-09-23 81 views
3

我试图在我的LibGdx AssetManager中加载我的.ttf字体,但似乎无法正确显示。我的尝试:LibgGdx - 在AssetManager中加载TrueType字体

里面我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 

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

回答

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

哦,我要做PARAMATERS那里,我知道了。你能给我示例代码如何将这个分配给我将放置在屏幕上的BitmapFont吗?类似于我的问题中的第二个代码示例 –

+0

通常,您将使用名称,在我的示例中使用'fontTest'并使用它从AssetManager获取字体,但因为此处名称为'“fontTest.ttf”“我不确定如何进行。我不能做正常的'(GdxAssetManager.fontTest,BitmapFont.class)' –

+0

好吧,刚刚添加到答案示例 –