【发布时间】:2014-03-05 12:10:35
【问题描述】:
我正在使用 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)
【问题讨论】:
-
长度为210。索引从0开始。所以数组没有元素@索引210。
-
我知道...但我与长度无关。我只是想显示一个文本“新高:8”。它不是 210 个字符
-
但您的堆栈跟踪暗示了我刚才提到的内容。
LC.Score是什么? -
LC.Score 为当前分数,整数最大2位
-
好的,我自己找到了答案……它与初始化有关。这里我如何初始化文本。 “分数:”所以这里以某种方式定义了最大字符,当我尝试显示“新高:8”时它太长了。相反,我做了不同的初始化(有很多空格)“分数:............”它就像魅力一样工作:)对不起,我不熟悉评论格式