2015-01-21 76 views
0

現在我正在爲角色製作技巧,我想添加冷卻時間,但我不知道如何設置時間,但我想我知道哪些變量它應該有:System.nanoTime()和其他與時間有關的類

private long currentTime; <-- this is the actual cooldown 
private long cooldownTime; <--- this is the time it must pass before its ready 
private boolean onCooldown; <---- game uses this to check if its on cooldown 
private long elapsed = System.nanoTime(); <-- this takes the exact time when a skill is used and is setOnCooldown. 

因此,這是基本的變量,但我不知道,我怎麼可以設置他們在所有的,我得到了一個update()方法,在遊戲中鑄造()方法。請senpais中止!讓巧克力餅乾的人願意HALPS NN

+0

System.nanoTime返回'long';不要將它轉換爲'float',否則你會遇到問題。 – immibis 2015-01-21 04:36:46

+0

提示Ty,更新它。 – 2015-01-21 04:37:37

+0

你真的需要nanotime嗎? timeMillis對於遊戲應該足夠好,並且在重新啓動時可能不一致。 – Thilo 2015-01-21 04:37:53

回答

0

TL;博士

Instant.now() 
     .plus(
      Duration.ofHours(1).plusMinutes(35) 
     ) 

詳細

不太清楚你的問題,但你似乎要跟蹤的時間跨度爲「降溫「,並且顯然在那段時間過去之後進行測試。

在Java中8使用java.time

的java.time類,後來包括DurationPeriod類用於跟蹤時間從時間軸獨立的跨度。

Duration duration = Duration.ofHours(1).plusMinutes(35); 

獲取UTC當前時刻,分辨率高達nanoseconds。在Java 8中,當前時刻被捕獲到milliseconds。在Java 9中,Clock的新實現捕獲Instant類的高達整個納秒分辨率的當前時刻。

Instant now = Instant.now(); 

要確定的那一刻時降溫到期,應用DurationInstant生成另一個Instant

Instant coolDownExpires = now.plus(duration); 

關於java.time

java.time框架是建立在Java 8和更高版本。這些類取代了日期時間類legacy,如java.util.Date,Calendar,& SimpleDateFormat

Joda-Time項目現在位於maintenance mode,建議遷移到java.time類。請參閱Oracle Tutorial。並搜索堆棧溢出了很多例子和解釋。規格是JSR 310

從何處獲取java.time類?

ThreeTen-Extra項目與其他類擴展java.time。這個項目是未來可能增加java.time的一個試驗場。您可以在這裏找到一些有用的類,如IntervalYearWeekYearQuartermore