我在这里听到答案:How get a string width in Libgdx?
我遇到的问题是getBounds似乎没有返回正确的宽度。下面的代码应该在字符串结尾之后放置点,但是你可以看到一些文字中出现的点。如何获得正确的文本宽度?
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;
}
答案 0 :(得分:2)
在调用另一个BitmapFont方法后,getBounds返回的TextBounds不保证有效。 TextBounds实例在内部重用以避免分配。这是一个不幸的问题,但这是libgdx避免使用GC的结果。