根据文档数组大小和索引,如果我们在array.begin()和array.end()方法之间编写循环,则不会改变.
所以我声明了快照数组
SnapshotArray balls;
我的删除方法是
private void removeVillianGroups(int color){
Ball[] ball=balls.begin();
for(int i=0;i
if(balls.get(i).getColor()==color){
ball.removeValue(balls[i],true);
}
}
balls.end();
}
我在施法时遇到错误
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.mygames.haloween.Entity.Ball;
在这一行上面
Ball[] ball=balls.begin();
所以在这里我创建了我的阵列,基本上我将屏幕宽度分成六个相等的部分,有6列球
private void createVillians() {
for (int color=0;color<4;color++)//create 4 diffirent color balls in 4 rows
for(int i=0;i<6;i++)//each row contan 6 same color balls
{
balls.add(new Ball(viewport,new Vector2(i*viewport.getWorldWidth()/6,
0-c*viewport.getWorldWidth()/6),color));
//Ball class just draw balls according their color between 0 to 3.
}
}
解决方法:
你不能只使用常规的ArrayList并向后迭代:
ArrayList balls;
private void removeVillianGroups(int color){
for(int i = balls.size() - 1; i >= 0; i--){
if(balls.get(i).getColor()==color){
balls.remove(i);
}
}
}
标签:java,arrays,libgdx
来源: https://codeday.me/bug/20190701/1350661.html