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 VertexBufferObjectManager pVertexBufferObjectManager) 


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

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

转载于:https://www.cnblogs.com/kylin17/archive/2013/03/06/2946216.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值