(Libgdx 1.6.1)由于索引超出范围,BitmapFontCache.draw崩溃 [英] (Libgdx 1.6.1) BitmapFontCache.draw crashing due to index out of bounds

查看:213
本文介绍了(Libgdx 1.6.1)由于索引超出范围,BitmapFontCache.draw崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将我的Libgdx项目从1.4.x更新为1.6.1。我在我的游戏中使用BitmapFontCache进行对话,使用BitmapFontCache.draw(开始,结束)按字符绘制字符串字符。这在1.4.x中工作正常,但在进行必要的更改以获得1.6.1构建之后,在显示最后一个字符后启用换行时,似乎会导致崩溃。奇怪的是,这似乎不是一个行字符串的问题。

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);

然后我增加字符数并绘制。 currentCharacter在根据字符串长度到达字符串末尾时停止:

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 );

这在1.4.x中工作正常,即使使用多行换行包但似乎导致了如果行换行到第二行(在绘制最后一个字符后崩溃),则为bounds exception。以下是导致SpriteBatch崩溃的行。

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);

我需要一种新方法来计算绘制字符串的长度吗?我是否需要以某种方式使用返回GlyphLayout?或者这可能是一个错误?

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?

推荐答案

好的,我知道问题出在哪里,我很确定这是一个错误在libgdx。

OK, I know where the issue lies, and I'm pretty certain it's a bug in libgdx.

我也有一个解决方法,虽然它有点hacky。

I also have a workaround, although it's a little hacky.

问题
GlyphLayout 在空格字符上包裹一行时,它会优化终止空间。因此,删除空格后,布局中的字形总数现在小于字符串中的字符数。包裹在空格字符上的线越多,两者之间的差异就越大。

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.

解决方法
In为了计算出用于渲染全文的长度,我们需要计算GlyphLayout中的字形数而不是字符串中的字符数。这里有一些代码可以做到......

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;
}

GlyphLayout to传入将是 BitmapFontCache.addText()方法返回的那个。

The GlyphLayout to pass in will be the one that was returned by the BitmapFontCache.addText() method.

这篇关于(Libgdx 1.6.1)由于索引超出范围,BitmapFontCache.draw崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆