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

답변

4

사용 javax.swing.Timer 애니메이션 속도를 조절. 타이머의 start()stop() 메서드를 호출하는 일반적인 컨트롤은 here입니다. 그러한 Timer을 사용하는 fleet simulation도 관심이있을 수 있습니다.

관련 문제