android - Andengine文本索引越界异常

标签 android andengine

我正在使用 andengine 编写游戏,我正在使用 Andengine Text 来显示高分...

这就是代码:

StrokeFont mFont = FontFactory.createStrokeFromAsset(this.getFontManager(), mainFontTexture, this.getAssets(), "Roboto-BoldItalic.ttf", 100, true, Color.WHITE, 2, Color.BLACK);
mFont.load();

text_score_menu = new Text(25, 25, mFont, "Score: ",getVertexBufferObjectManager());


if(LC.Score>Constants.highScore){
     prefs.edit().putInt("highScore",LC.Score).commit();
     text_score_menu.setText("New High: " + LC.Score);
}else{
     text_score_menu.setText("Score: " + LC.Score);
}

问题是,当出现新的高分时,我会在这里得到一个新的异常:

text_score_menu.setText("New High: " + LC.Score);

但是当分数不是新的高分并且分数显示为

时我没有那个问题
text_score_menu.setText("Score: " + LC.Score);

这是错误信息:

FATAL EXCEPTION: UpdateThread
    java.lang.ArrayIndexOutOfBoundsException: length=210; index=210
            at org.andengine.entity.text.vbo.HighPerformanceTextVertexBufferObject.onUpdateVertices(HighPerformanceTextVertexBufferObject.java:121)
            at org.andengine.entity.text.Text.onUpdateVertices(Text.java:335)
            at org.andengine.entity.text.Text.setText(Text.java:223)

最佳答案

这是 AndEngine 中文本的常见缺陷 - 当您实例化 text_score_menu 实体时,您设置了最大长度。尝试改变这个

text_score_menu = new Text(25, 25, mFont, "Score: ",getVertexBufferObjectManager());

text_score_menu = new Text(25, 25, mFont, "新高: 123456789",getVertexBufferObjectManager());

这将建立一个足够长的文本字段 - 然后确保在显示实体之前“设置”正确的文本 -(就像您在所提供的代码中所做的那样)

关于android - Andengine文本索引越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22197369/

相关文章:

javascript - 如何在关闭和打开后在 react native 应用程序上保持登录状态

java - 为什么重置我的适配器不使用新的 ArrayList?

android - 带有标签的 AndEngine 菜单项

android - Andengine 为 sprite 中的每个像素设置 alpha

android - Andengine,找不到使用倾斜/加速度计的方法

android - AndEngine MenuScene - 无法点击按钮

android - 如何手动生成 google-services.json

android - Android 8.0及以上版本权限策略更新有什么好处?

android - 显示当前聚焦的 View (即使没有布局选择器)

Andengine 2d 横向卷轴游戏,如马里奥