2014-06-14 26 views
1

我想添加暂停/恢复到我的游戏的可能性。不幸的是,我的解决方案只是暂停游戏,但不会恢复(当我点击恢复按钮时什么都没有发生 - 图像仍然存在)。我也尝试在thread上调用wait/notify方法,但它也不起作用 - 我得到了Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException。暂停/恢复的最佳解决方案是什么?摆动动画暂停和恢复

游戏循环

public void run() { 
    init(); 

    long startTime; 
    long reTime; 
    long waitTime; 

    while (running) { 
     startTime = System.nanoTime(); 
     int CarPosX = car.zwrocPolozenieX(); 
     int CarPosY = car.zwrocPolozenieY(); 
     update(b, CarPosX, CarPosY); 
     render(b); 
     //draw(); 
     repaint(); 


     if(b<4390) { 
      b=b+szybkoscMapy; 
     } 

     reTime = System.nanoTime() - startTime; 
     waitTime = targetTime - reTime/100000000; 
     try { 
      Thread.sleep(waitTime); 
     } 
     catch(Exception e) { 

     } 
    } 
} 

public void init() { 
    if(thread == null) { 
     thread = new Thread(this); 
     thread.start(); 
    } 
    b=0; 
    running = true; 
    image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); 
    g = (Graphics2D) image.getGraphics(); 


    map = new Map(levelNumber); 
    car = new Car(map); 
    car.setxpos(338); 
    car.setypos(150); 

} 

听众

pause_button.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      bp.running = false; 
     } 
    }); 

resume_button.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      bp.running = true; 
     } 
    }); 

回答