0

Right now I'm making skills for characters and I wanted to add cooldowns, but I have no idea on how to set times but I think I got an idea on which variables it should have:

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.

So this are the basic variables but I have no idea at all on how I could set them, I got an update() method, a cast() method inside the game. Please senpais halps! Giving choco cookies for anyone willing to halps n.n

  • System.nanoTime returns a `long`; don't convert it to `float` or you'll have problems. – user253751 Jan 21 '15 at 04:36
  • Ty for the tip, updated it. – Shiroi Tamashi Jan 21 '15 at 04:37
  • Do you really need nanotime? timeMillis should be good enough for games, and nanotime may not be consistent across restarts. – Thilo Jan 21 '15 at 04:37
  • That's good to hear, and no I'm not going just with one way, there are others like the one you mentioned, that's why I'm asking about that one and other related classes, if you can post an example with timeMillis I would def try it out, I just dont know anything about time classes in java, thanks Thilo! – Shiroi Tamashi Jan 21 '15 at 04:40
  • @Thilo Do not use `currentTimeMillis` unless you want your game to behave oddly when something changes the computer's time (including network time synchronization). – user253751 Jan 21 '15 at 04:42
  • :O oh I didnt knew that! and btw could you upvote the question? so more pople can see it? that would be very appreciated, or if you could give me a code example n.n – Shiroi Tamashi Jan 21 '15 at 04:47
  • @immibis: But won't nanoTime be even worse and lose track when you reboot the computer? – Thilo Jan 21 '15 at 04:51
  • @Thilo yes, but think about it for a moment - how can you reboot the computer without closing the game anyway? – user253751 Jan 21 '15 at 04:54
  • @immibis: If it's a game like FarmVille or the Tapped Out or all the other "get more coins in 24 hours", then you close the game a lot... – Thilo Jan 21 '15 at 04:55

1 Answers1

0

tl;dr

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

Details

Not quite sure of you Question, but you seem to want to track a span of time for "cool down", and apparently test when that time has passed.

Using java.time

The java.time classes in Java 8 and later include the Duration and Period classes to track a span of time unattached from the timeline.

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

Get the current moment in UTC with a resolution up to nanoseconds. In Java 8, the current moment is captured up to milliseconds. In Java 9, a new implementation of Clock captures the current moment in up to the full nanosecond resolution of the Instant class.

Instant now = Instant.now();

To determine the moment when that cool-down expires, apply the Duration to the Instant to generate another Instant.

Instant coolDownExpires = now.plus( duration );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154