java – 文本旋转libgdx

我想在libgdx中旋转BitmapFont.有关于这个问题的主题.
Draw a BitmapFont rotated in libgdx然而,我尝试的第一个解决方案是剪切我的文本,整个文本没有出现(斜切).当它说应该是180度时,它也会旋转90度.我只是不明白它.

码:

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();
}

最佳答案 一种方法是使用scene2d标签.它们非常容易使用,如果你在
Scene2D UI
Labels上阅读,你应该能够做你想做的事.然而,有些人不喜欢使用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);
}

我希望其中一些对您有用.

点赞