Draw a BitmapFont rotated in libgdx
我似乎无法弄清楚如何正确旋转位图字体。我认为您修改了SpriteBatch的转换矩阵。但是,尝试旋转会使文本绕某个点旋转,而我不知道如何相对于文本本身旋转它。
您可以将字形创建为sprite。
这样,您就可以将您的文本当作sprite使用。
示例代码:
注意,这将返回单个字形的Sprite。 (例如,将char'A'转换为sprite。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** 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; } |
您可以尝试以下代码:
1 2 3 4 5 6 7 8 9 10 11 | 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(); |
Lunatikul的第一个答案不适用于我的2D案例。它将我的文字减少到只有半个字母。我成功完成了以下任务:
1 2 3 4 | batch.begin(); batch.setTransformMatrix(new Matrix4().setToRotation(0,0,1,<insert angle here>)); font.draw(batch,"Hallo Welt", 100, 100); batch.end(); |
我只想添加..我想您在某些地图集中有字体基础图像..所以您需要添加TextureRegion原始sot gliph src,因为它只是相对于给定的Texture区域,所以
1 2 3 4 5 | 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); |