2015-09-23 82 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

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