【问题标题】:Andengine text indexoutofbounds exceptionAndengine 文本 indexoutofbounds 异常
【发布时间】: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”时它太长了。相反,我做了不同的初始化(有很多空格)“分数:............”它就像魅力一样工作:)对不起,我不熟悉评论格式

标签: android andengine


【解决方案1】:

这是 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());

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

【讨论】:

  • 我们同时找到了答案.. 无论如何谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 2021-01-12
  • 2011-11-10
相关资源
最近更新 更多