2015-01-21 75 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