Libgdx Android 上的文字

Libgdx text on Android

我是 libgdx 的新手,我正在尝试在屏幕上显示一些文本。

我看了几个教程,他们都说我应该启动 BitmapFont 不带参数,然后它将使用默认字体。但在我的 android 设备上,它只显示这些黑色矩形:

这是我的代码:

public class GameOverScreen implements Screen {
    private JumpGame game;
    private OrthographicCamera cam;

    private BitmapFont gameOverFont;
    private Label.LabelStyle labelStyle;
    private Label label;

public GameOverScreen(JumpGame game) {
    this.game = game;
    cam = new OrthographicCamera();

    gameOverFont = new BitmapFont();
    labelStyle = new Label.LabelStyle(gameOverFont, Color.BLACK);
    label = new Label("Game Over", labelStyle);
}

@Override
public void show() {
    cam.setToOrtho(false, game.getWidth() / 4, game.getHeight() / 4);
    game.batch.setProjectionMatrix(cam.combined);
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    game.batch.begin();
    label.draw(game.batch, 1);
    game.batch.end();
}

@Override
public void resize(int width, int height) {

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {

}
}

编辑:

好的,我正在使用来自第二个线程的文本启动屏幕。当我启动它时 "normally" 它工作正常。有人可以解释这是为什么吗?如果我仍然想从线程启动它,该如何解决?

根据 wiki

To pass data to the rendering thread from another thread we recommend using Application.postRunnable(). This will run the code in the Runnable in the rendering thread in the next frame, before ApplicationListener.render() is called.


标签是 libgdx 的 2D 场景图的一部分,因此最好与舞台一起使用。

如果你想绘制字体,你可以简单地借助Batch来绘制。

font.draw(batch,"Hello World",100,100);