AndEngine 动态更新Text文本内容时报ArrayIndexOutOfBoundsException错误的解决

在游戏中,文本是必不可少的元素之一,通常创建了一个文本内容,还有可能会随时更改它,创建一个文本的方法如下(摘至Andengine源码中的TextExample.java):

 

this.mFont = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32);
this.mFont.load();

final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
final Text centerText = new Text(100, 40, this.mFont, "Hello AndEngine!\nYou can even have multilined text!", new TextOptions(HorizontalAlign.CENTER), vertexBufferObjectManager);
final Text leftText = new Text(100, 170, this.mFont, "Also left aligned!\nLorem ipsum dolor sit amat...", new TextOptions(HorizontalAlign.LEFT), vertexBufferObjectManager);
final Text rightText = new Text(100, 300, this.mFont, "And right aligned!\nLorem ipsum dolor sit amat...", new TextOptions(HorizontalAlign.RIGHT), vertexBufferObjectManager);

此时,如果调用Text的setText(CharSequence text)方法更改文字内容且文字内容比原来长的时候,会看到Logcat中的ArrayIndexOutOfBoundsException异常信息,同样Google了一下,在Andengine讨论区找到了解决方案:

使用Text的另外一个构造函数

public Text(final float pX, final float pY, final IFont pFont, final CharSequence pText, final int pCharactersMaximum, final VertexBufferObjectManager pVertexBufferObjectManager) 


指定最小的文字内容长度,使文本内容长度不超过这个即可。

另外也在开源中国找到了这个解决方案和Text使用的一些总结,请参阅:AneEngine Text的使用心得微笑

posted @ 2013-03-06 15:34  路人 乙  阅读(452)  评论(0编辑  收藏  举报