2011-12-14 110 views
6

我似乎無法弄清楚如何正確旋轉一個Bitmap字體。我認爲你修改了SpriteBatch的轉換矩陣。然而,試圖旋轉旋轉文本週圍的某個點,我不知道如何相對於文本本身旋轉它。繪製一個BitmapFont在libgdx中旋轉

回答

4

你可以試試下面的代碼:

Matrix4 mx4Font = new Matrix4(); 
BitmapFont font; 
SpriteBatch spriteFont; 

font = new BitmapFont(Gdx.files.internal("data/font/agencyFB.fnt"), Gdx.files.internal("data/font/agencyFB.png"), true); //must be set true to be flipped 
mx4Font.setToRotation(new Vector3(200, 200, 0), 180); 
spriteFont.setTransformMatrix(mx4Font); 
spriteFont.begin(); 
font.setColor(1.0f, 1.0f, 1.0f, 1.0f); 
font.draw(spriteFont, "The quick brown fox jumped over the lazy dog", 100, 110); 
spriteFont.end(); 
+3

你應該做setToRotation – chairbender 2014-07-18 04:20:39

+1

我沒有工作的時候是什麼意思載體闡述,我在這裏失蹤? – 2015-05-15 10:28:56

5

您可以創建一個字形到一個精靈。這樣,你可以將你的文本操作爲精靈。

示例代碼:

請注意,這將返回單個字形的Sprite。 (如字符「A」轉化成一個精靈。)

/** Creates a sprite from a glyph. 
* 
* @param ch 
* @return Sprite 
*/ 
public Sprite getGlyphSprite (char ch) { 

    Glyph glyph = Globals.g.font.getData().getGlyph(ch); 
    Sprite s = new Sprite(Globals.g.font.getRegion().getTexture(), 
      glyph.srcX,glyph.srcY,glyph.width, glyph.height); 

    s.flip(false, true); 
    s.setOrigin(glyph.width/2, glyph.height/2); 

    return s; 
} 
0

我只想補充..我想你有一些圖冊內的字體的基本圖像..所以你需要添加TextureRegion原件SOT gliph SRC因爲它只是相對於給定的紋理區域所以

BitmapFont font = ... 
BitmapFont.Glyph glyph = font.getData().getGlyph(ch); 
int srcX = glyph.srcX + font.getRegion().getRegionX(); 
int srcY = glyph.srcY+ font.getRegion().getRegionY(); 
Sprite s = new Sprite(font.getRegion().getTexture(), srcX,srcY,glyph.width, glyph.height); 
0

Lunatikul的第一個答案沒有在我的2D情況下工作。它將我的文本只切成半個字母。我是全成下列要求:

batch.begin(); 
batch.setTransformMatrix(new Matrix4().setToRotation(0,0,1,<insert angle here>)); 
font.draw(batch, "Hallo Welt", 100, 100); 
batch.end();