2011-12-14 60 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();