我正在做某种打砖块,并且卡住了。我这样做的方式是通过JFrame,JPanel和Timer。所以,我做什么,每个定时器更新 这游戏检查碰撞和从ArrayList中删除对象
public class Controller implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
ball.move();
desk.move();
deskCollision();
squareCollision();
repaint();
}
}
我创造了广场的ArrayList,我打印了出来。当我检查与正方形和球的碰撞时,它会起作用。所以现在我想删除一个特定的方块,当一个球击中它并改变球的方向时。首先,我尝试了这种方式,没有任何形式的循环。
if(ListOfSquares.get(24).getBounds2D().intersects(ball.getBounds2D())){
ball.dy = 1;
ball.dx = -1;
ListOfSquares.remove(24);
}
这也适用。但是,因为我想制作一个循环,它会遍历所有的方格,并且总是删除特定的方格,所以我迷了路。我已经做了这个样子,但 它的错误结束了 - 在线程“AWT-EventQueue的 - 0” java.util.ConcurrentModificationException例外 -
for(Square square : ListOfSquares){
int index = ListOfSquares.indexOf(square);
if (ball.getBounds2D().intersects(square.getBounds2D())) {
if(ball.dx == -1 && ball.dy == -1){
ball.dy = 1;
ball.dx = -1;
ListOfSquares.remove(index);
}
//etc...
}
}
感谢您的帮助。
,当你重复它 –