我正在製作一個射擊遊戲,作爲敵人在演員中隨機射擊的項目。但每當敵人隨機射擊時,就會拋出java.util.ConcurrentModificationException。 這是拍攝隨機java在我的代碼中拋出java.util.ConcurrentModificationException
public void enemyAttackStrategy() {
// Fire at when at around the 1/4, 1/2 and 3/4 in a random direction
int width = Gui.getInstance().getWidth();
int firstPoint = width/4 ;
int secondPoint = firstPoint * 2;
int thirdPoint = firstPoint * 3;
int dist = 2;
boolean nearFirst = (getX() - firstPoint) < 3 && (getX() - firstPoint > 0) ;
boolean nearSecond = (getX() - secondPoint) < 3 && (getX() - secondPoint > 0) ;
boolean nearThird = (getX() - thirdPoint) < 3 && (getX() - thirdPoint > 0);
if(nearFirst || nearSecond || nearThird){
//System.out.println("near!!!!!!!!" + (firstPoint) + " " + (secondPoint) + " " + (thirdPoint));
Game.getInstance().enemyShot();
}
的代碼和創建enemybullet代碼
public void enemyShot() {
Bullet bullet = new Bullet("EnemyBullet", "Bullet.png", 14);
bullets.add(bullet);
int minSpeed = -15;
int xPosition = enemyShip.getX();
int yPosition = enemyShip.getY();
int xSpeed = random.nextInt(30) + minSpeed;
int ySpeed = random.nextInt(30) + minSpeed;
addToWorld(bullet, xPosition, yPosition, xSpeed, ySpeed, 2);
//System.out.println("Added Enemy Bullet");
}
這是for循環的指我
public void timer() {
for (Tame oneTame : tames) {
tame.timeTick();//}
}
,這是錯誤
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:886)
at java.util.ArrayList$Itr.next(ArrayList.java:836)
at GameFrameworkJavaFX.Game.timeTick(Game.java:135)
您尚未發佈所有相關代碼,請告訴我們正在調用'enemyAttackStrategy()':)的循環 – alfasin 2015-03-08 21:38:31
調用enemyAttackStrategy()的循環處於敵方階級中。但java.util.ConcurrentModificationException引用了我在遊戲類中的for循環。 – 2015-03-08 21:43:37
發佈[SSCCE](http://sscce.org/)。也看看http://stackoverflow.com/questions/223918/iterating-through-a-list-avoiding-concurrentmodificationexception-when-removing – Pshemo 2015-03-08 21:44:19