2015-04-06 38 views
0

我需要一種方法來在設定的時間在關卡中產生物體。我知道我可以用If語句通過檢查時間變量來做到這一點,但是這個想法很愚蠢,因爲它會檢查ewery更新,如果它的時間合適,這會讓我的遊戲變慢。有另一種方法嗎?我使用Java進行編程。對於糟糕的英語感到抱歉。設定時間在遊戲關卡中產卵物體

+2

你能不能給我們你寫的語言?嘖。 –

+0

我正在用Java寫作 – Simas

回答

2

你要使用Java的定時器類,http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html

這裏有一個簡單的例子:

public class Reminder 
{ 
    Timer timer; 

    public Reminder(int seconds) { 
     timer = new Timer(); 
     timer.schedule(new RemindTask(), seconds*1000); 
    } 

    class RemindTask extends TimerTask { 
     public void run() { 
      System.out.println("Time's up!"); 
      timer.cancel(); //Terminate the timer thread 
     } 
    } 

    public static void main(String args[]) { 
     new Reminder(5); 
     System.out.println("Task scheduled."); 
    } 
} 

在你的情況下,你會想,可以替代秒參數計時器時間表方法調用到Date變量。您將使用此構造函數:

schedule(TimerTask任務,日期時間) 計劃指定的任務在指定的時間執行。

希望這可以幫助你!

+0

謝謝!這是我需要的。 – Simas