问题
I just recently updated my Libgdx project from 1.4.x to 1.6.1. I use BitmapFontCache for my dialogue in my game, drawing a string character by character using BitmapFontCache.draw(start, end). This was working fine in 1.4.x but after making the necessary changes to get 1.6.1 to build, it seems to cause a crash when wrapping is enabled after the last character is displayed. Strangely this does not seem to be a problem with one line strings.
Here is how I add my text:
fontCache.addText( message, fontPosX, fontPosY, fontWidth, Align.left, true);
Then I increment the character count and draw. currentCharacter stops when reaching the end of the string based on its length:
fontCache.draw( batch, 0, currentCharacter );
This worked fine in 1.4.x even with multi-line wrapped strings but seems to cause an out of bounds exception if the lines wraps to a second line (crashes after drawing the last character). Here is the line causing crash in SpriteBatch.
System.arraycopy(spriteVertices, offset, vertices, idx, copyCount);
Is there a new way I need to be calculating the length of the string for drawing? Do I need to use the return GlyphLayout in some way? Or is this perhaps a bug?
回答1:
OK, I know where the issue lies, and I'm pretty certain it's a bug in libgdx.
I also have a workaround, although it's a little hacky.
The Problem
When GlyphLayout
wraps a line on a space character, it optimises out the terminating space. So with the space removed, the total number of glyphs in the layout is now less than the number of characters in the string. The more lines that get wrapped on a space character, the bigger the discrepency will be between the two.
The Workaround In order to work out what length to use for rendering the full text therefore, we need to count the number of glyphs in the GlyphLayout instead of the number of characters in the String. Here's some code that does that...
private int calcLength(GlyphLayout glyphLayout) {
int length = 0;
for(GlyphLayout.GlyphRun run : glyphLayout.runs) {
length += run.glyphs.size;
}
return length;
}
The GlyphLayout
to pass in will be the one that was returned by the BitmapFontCache.addText()
method.
来源:https://stackoverflow.com/questions/30618019/libgdx-1-6-1-bitmapfontcache-draw-crashing-due-to-index-out-of-bounds