2014-06-14 479 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; 
     } 
    }); 

回答