我用Java写了一个简单的游戏。这是主要代码:
public class MainPanel extends JPanel {
private Player player = new Player(100, 100, 3, 3);
private Point2D targetPoint = new Point2D.Float(130, 350); //Pos on begin
private ArrayList<Beam> beams = new ArrayList<Beam>();
public MainPanel() {
setPreferredSize(new Dimension(300, 400));
addMouseMotionListener(new MouseMotionHandler());
//Add shortcuts
makeShortcut("player.BM1", "F1", new SetBeamModeAction(1));
makeShortcut("player.BM2", "F2", new SetBeamModeAction(2));
//Start threads
Thread t = new Thread(new PlayerMoveRunnable());
t.start();
Thread t2 = new Thread(new PlayerShootRunnable());
t2.start();
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, 300, 400);
//Draw player
g2.drawImage(player.getImage(), (int)player.getX(), (int)player.getY(), null);
//Draw beams
for (Beam beam : beams) {
g2.drawImage(beam.getImage(), (int)beam.getX(), (int)beam.getY(), null);
}
}
//Thread running all the time
private class PlayerMoveRunnable implements Runnable {
public void run() {
try {
while (true) {
player.moveToPoint(targetPoint);
repaint();
Thread.sleep(15);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//Thread working all the time
private class PlayerShootRunnable implements Runnable {
public void run() {
try {
while (true) {
//Choose which beam to shoot (depends on set mode)
Thread t;
switch (player.getBeamMode()) {
case 1:
t = new Thread(new BeamMoveRunnable(new Beam1(player.getX()+18, player.getY(), 0, -15)));
break;
case 2:
t = new Thread(new BeamMoveRunnable(new Beam2(player.getX()+18, player.getY(), 0, -30)));
break;
default:
t = null;
break;
}
t.start();
Thread.sleep(200);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private class BeamMoveRunnable implements Runnable {
private Beam beam;
public BeamMoveRunnable(Beam beam) {
this.beam = beam;
}
public void run() {
Beam beam = this.beam;
beams.add(beam);
try {
while (true) {
if (beam.getY() <= 0) {
beams.remove(beam);
break;
}
beam.move();
repaint();
Thread.sleep(20);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
[它不是完整的代码。我剪了几行肯定不会引起问题]
我收到这样的错误:
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
at java.util.ArrayList$Itr.next(ArrayList.java:791)
at spacecommander.MainPanel.paintComponent(MainPanel.java:53)
at javax.swing.JComponent.paint(JComponent.java:1054)
等等...
问题出在哪里?我知道 ConcurrentModificationException 是什么意思,但我不知道问题出在哪里。也许我应该进行一些同步。如果是,请显示在哪里