font.draw() works correctly on first call but not on second

余生长醉 提交于 2020-01-05 05:27:10

问题


I'm not sure if you're not allowed to call font.draw() twice but the first linedisplays correctly but the second one is very glitchy, a lot of letters are missing [that appear fine in the first line] and random numbers seem to not draw. My .fnt file is fine as it works on buttons and checkbox's just fine.

Here's some of my code:

private BitmapFont font = new BitmapFont(Gdx.files.internal("test.fnt"),false);
private SpriteBatch batch = new SpriteBatch();
//in Render Method

    batch.begin();

    font.draw(batch, "Best Distance: " + bestDistance + "m", Gdx.graphics.getHeight() / 2, Gdx.graphics.getWidth() / 2);

    font.draw(batch, "Distance: " + finalDistance + "m", Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);

    stage.draw();


    hud.stage.draw();


    batch.end();

If I switch the order of their draw call then whichever one is called first draws correctly.


回答1:


You're calling stage.draw() without closing the spritebatch. Simply call batch.end() before starting to draw the stage. Rendering a batch inside another opened batch will result in all kind of weird behaviour that makes you think the fault lies in everything but the batch.

This is a similar problem.



来源:https://stackoverflow.com/questions/52800477/font-draw-works-correctly-on-first-call-but-not-on-second

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!