问题
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