文本旋转 libgdx

4
我想在libgdx中旋转BitmapFont。这个问题已经有相关的话题。 Draw a BitmapFont rotated in libgdx 然而,我尝试的第一个解决方案截断了我的文本,整个文本没有出现(对角线割)。 它还将其旋转90度,而它应该是180度。 我真的不明白。
代码:
public void show() {
    sr = new ShapeRenderer();
    w = Gdx.graphics.getWidth();
    h = Gdx.graphics.getHeight();
    textRotation = new Matrix4();
    textRotation.setToRotation( new Vector3(200,200, 0), 180);

    rectThickness = h / 120;
    c1 = Color.WHITE;
    c2 = Color.WHITE;
    f = new BitmapFont(Gdx.files.internal("fonts/MyFont.fnt"),
             Gdx.files.internal("data/MyFont.png"), true);
    f.setScale(h/500);
    sb = new SpriteBatch();
}
private void renderPlayerScores() { //callend in render
    sb.setTransformMatrix(textRotation);
    f.setColor(players[0].getColor());
    sb.begin();
    f.draw(sb, "Player 1: "+ Integer.toString(scores[0]), 100, 110);
    sb.end();
}

你可以使用缓动动画,但那需要你自己创建文本精灵访问器。 - Kumar Saurabh
请给我举个例子好吗? - gx10001
你找到解决方案了吗? - Boni2k
1个回答

3

一个做法是使用Scene2d标签。它们非常易于使用,如果您阅读了有关Scene2D UILabels的内容,您应该能够做到想要的效果。然而,有些人不喜欢使用Scene2D。以下代码可以实现您想要的效果,但这需要一些额外的工作,只是为了避免使用Scene2D。

public SpriteBatch spriteBatch;
private int posX = 100;
private int posY = 100;
private float angle = 45;
private String text = "Hello, World!";
private BitmapFont font;
private Matrix4 oldTransformMatrix;
Matrix4 mx4Font = new Matrix4();

@Override
public void show() {
    font = new BitmapFont(Gdx.files.internal("someFont.ttf"));
    oldTransformMatrix = spriteBatch.getTransformMatrix().cpy();
    mx4Font.rotate(new Vector3(0, 0, 1), angle);
    mx4Font.trn(posX, posY, 0);
}

@Override
public void render() {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    spriteBatch.setTransformMatrix(mx4Font);
    spriteBatch.begin();
    font.draw(spriteBatch, text, 0, 0);
    spriteBatch.end();
    spriteBatch.setTransformMatrix(oldTransformMatrix);
}

我希望以下内容对您有所帮助:

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接