2013-07-12 26 views
0

我有一個遊戲,包括遊戲進行時間,我的意思是你玩遊戲和時間滴答作爲你玩,當它完成它停止遊戲。但是我不能同時運行兩個代碼線程......有什麼辦法可以解決這個問題嗎?如何在java遊戲中計時?

這裏是我的計時器代碼:

public class Time { 
private static String gl; 
private static int gli; 
public static int weeks = 0; 

public static void main(String[] args) throws InterruptedException { 
    startdays(); 
} 
public static void startdays() throws InterruptedException { 
    gl = JOptionPane.showInputDialog("How many weeks? do you want the game to to on for?\nPlease type numbers, Or game will crash.\nEach week is 10 seconds long."); 
    gli = Integer.parseInt(gl); 
    gli++; 

    for (int i = 0; i < gli; i++) {   
     if(weeks < gli){ 
      Thread.sleep(10000); 
      weeks++; 
     }else if(weeks == gli){ 
      JOptionPane.showMessageDialog(null, "Uh oh, Your time in office is up. You died of an infectuous disease.");     
     } 
    } 
} 
} 

在另一個I類調用該方法,像這樣

JOptionPane.showMessageDialog(frame, compname); 
    Time time = new Time(); 
    time.startdays(); 
    JOptionPane.showMessageDialog(frame, "Im still here"); 

那麼,有沒有辦法有計時器和比賽在同一時間去?

+0

1)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 2)源代碼中的單個空白行是* always *就足夠了。 –

回答