我有两个我正在使用的 ArrayList,一个用于敌人的精灵,另一个用于子弹。当我运行该应用程序时,它偶尔会在模拟器上崩溃,并且在几个级别内它会在碰撞时在设备上崩溃。日志不断告诉我我有一个简单的 ListIterator 错误。我想知道如何实现 ListIterator。我没有使用 ListIterator,我四处寻找信息和示例,但我对如何实际操作有点困惑。add() 函数在我的视图类的 onTouch 方法中被调用,并且碰撞发生在我的线程类内部的它自己的碰撞方法中。
日志:
07-20 19:57:46.604: ERROR/AndroidRuntime(234): Uncaught handler: thread Thread-9 exiting due to uncaught exception
07-20 19:57:46.613: ERROR/AndroidRuntime(234): java.util.ConcurrentModificationException
07-20 19:57:46.613: ERROR/AndroidRuntime(234): at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:64)
07-20 19:57:46.613: ERROR/AndroidRuntime(234): at com.android.hitmanassault.HitmanView$HitmanThread.startGame(HitmanView.java:333)
07-20 19:57:46.613: ERROR/AndroidRuntime(234): at com.android.hitmanassault.HitmanView$HitmanThread.gameStart(HitmanView.java:290)
07-20 19:57:46.613: ERROR/AndroidRuntime(234): at com.android.hitmanassault.HitmanView$HitmanThread.updateGame(HitmanView.java:393)
07-20 19:57:46.613: ERROR/AndroidRuntime(234): at com.android.hitmanassault.HitmanView$HitmanThread.run(HitmanView.java:237)
检查碰撞:
private void startGame(){
synchronized(mSurfaceHolder){
for(Beam bullet: beam){
for(Sprite sprite: sprites){
if(checkCollision(sprite, bullet)){
sprites.remove(sprite);
beam.remove(bullet);
mScore = mScore + 1;
break;
}
}
}
}
}
碰撞方法:
public boolean checkCollision(Sprite sprite, Beam bullet){
boolean retValue = false;
int SpriteX = sprite.getX();
int SpriteY = sprite.getY();
int SpriteXS = sprite.getX() + sprite.getWidth();
int SpriteYS = sprite.getY() + sprite.getHeight();
int BeamX = bullet.getX();
int BeamY = bullet.getY();
int BeamXS = bullet.getX() + bullet.getBitmap().getWidth();
int BeamYS = bullet.getY() + bullet.getBitmap().getHeight();
if ((BeamX >= SpriteX && BeamX <= SpriteXS) || (BeamXS >= SpriteX && BeamXS <= SpriteXS)) {
if ((BeamY >= SpriteY && BeamY <= SpriteYS) || (BeamYS >= SpriteY && BeamYS <= SpriteYS)) {
retValue = true;
}
}
return retValue;
}