我目前正在开发一款简单的2D图形游戏。我刚刚添加了碰撞检测,虽然它有效,但它返回ConcurrentModificationException
。
These are the errors I get.
当鼠标退出屏幕时,我的游戏也不会暂停。此外,每当发生碰撞时,游戏中也存在1/2到1秒滞后,这可能是错误的结果。此外,当我点击所有气泡时,气泡不会继续被绘制。此外,如果您发现任何效率错误,请告诉我。我在下面添加了相关代码。非常感谢你。
public class Game extends JPanel{
public static final int WINDOW_WIDTH = 600;
public static final int WINDOW_HEIGHT = 400;
private boolean insideCircle = false;
private static boolean ifPaused = false;
private static JPanel mainPanel;
private static JLabel statusbar;
private static int clicks = 0;
public static Bubbles[] BubblesArray = new Bubbles[5];
public static List<Bubbles> bubbles = new ArrayList<Bubbles>();
public Game() {
for (int i = 0; i < 10 ; i++){
bubbles.add(new Bubbles());
}
}
public void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
if (!ifPaused){
for(Bubbles b : bubbles) b.paint(graphics);
}
}
public void update() {
if (!ifPaused){
for (Bubbles b : bubbles) b.update();
}
}
public static void main(String[] args) throws InterruptedException {
statusbar = new JLabel("Default");
Game game = new Game();
JFrame frame = new JFrame();
game.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
statusbar.setText(String.format("Your mouse is at %d, %d", e.getX(), e.getY()));
for (Bubbles b : bubbles)
{
int x = b.getX();
int y = b.getY();
int r = b.getCircleSize();
if ( (e.getX() >= (x - r/2) && e.getX() <= (x+ r/2)) && (e.getY() >= (y - r/2) && e.getY() <= (y+ r/2)) ){
//System.out.println("Inside");
bubbles.remove(b);
}
}
}
public void mouseExited(MouseEvent e){
ifPaused = true;
}
public void mouseEntered(MouseEvent e){
ifPaused = false;
}
});
frame.add(game);
frame.add(statusbar, BorderLayout.SOUTH);
frame.setVisible(true);
frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle("Bubble Burst");
frame.setResizable(false);
frame.setLocationRelativeTo(null);
while (true) {
if (!ifPaused){
game.update();
game.repaint();
Thread.sleep(5);
}
}
}
}