Andengine文本indexoutofbounds异常

时间:2014-03-05 12:10:35

标签: 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)

1 个答案:

答案 0 :(得分:8)

这是AndEngine中常见的文本陷阱 - 当您实例化text_score_menu实体时,则设置最大长度。尝试更改此

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

text_score_menu = new Text(25, 25, mFont, "New High: 123456789",getVertexBufferObjectManager());

将建立足够长的文本字段 - 然后确保在显示实体之前“设置”正确的文本 - (正如您在所呈现的代码中所做的那样)