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

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

问题描述

我最近刚刚将我的 Libgdx 项目从 1.4.x 更新到 1.6.1.我在游戏中使用 BitmapFontCache 进行对话,使用 BitmapFontCache.draw(start, end) 逐个字符地绘制字符串.这在 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 中运行良好,即使使用多行包装的字符串,但如果行换行到第二行(绘制最后一个字符后崩溃)似乎会导致越界异常.这是导致 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.

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

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 将是 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天全站免登陆