LibGDX getBounds not returning correct width

纵饮孤独 提交于 2020-01-05 13:10:31

问题


I am following the answer here: How get a string width in Libgdx?

The problem I am having is that the getBounds doesn't seem to return to correct width. My code below should place dots after the end of the string, but you can see dots bleeding through some of the text. How do I get correct width of the text?

for (HighScore s : this.scoresData){
    font.draw(batch, s.name, x, y);
    TextBounds nameBounds = font.getBounds(s.name);

    font.draw(batch, s.value, (float) (KineticAssaultMain.WIDTH / 1.5f), y);

    for (float i = (nameBounds.width + x); i < ((float)Screen.WIDTH / 1.5f);){
        TextBounds dot = font.draw(batch, ".", i, y);
        i += dot.width;
    }

    // go to the next line
    y -= 32;
}

回答1:


The TextBounds returned from getBounds isn't guaranteed to be valid after calling another BitmapFont method. The TextBounds instance is reused internally to avoid allocation. It's an unfortunate gotcha but is a result of libgdx avoiding GC.



来源:https://stackoverflow.com/questions/20750984/libgdx-getbounds-not-returning-correct-width

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