1

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?

enter image description here

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;
}
Community
  • 1
  • 1
James
  • 538
  • 1
  • 6
  • 23
  • It seems the "-" and the numbers are the problem, like if their particular bounds were wrong, and they certainly look like from a bigger font. – Daahrien Dec 23 '13 at 21:12
  • Odd. Do you get this problem in all fonts, or just this particular one? – R Hyde Dec 23 '13 at 21:24
  • weird. I used the hiero font editor, so I assumed the fonts would work nicely with LibGDX's TextBounds. this is the only font I am using. – James Dec 24 '13 at 02:35

1 Answers1

2

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.

NateS
  • 5,751
  • 4
  • 49
  • 59