0

I am making a game in java, And I was wondering how I would get the delta time so the images and such rendered at the correct speed? The code I have does not work, and all it returns when printed out in a loop is 0.

public static long getTime() {
    return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}

public static int getDelta() {
    long time = getTime();
    int delta = (int) (time - lastFrame);
    lastFrame = time;

    return delta;
}
  • possible duplicate of [Monotonically increasing time in Java?](http://stackoverflow.com/questions/434369/monotonically-increasing-time-in-java) – Cairnarvon Jan 16 '15 at 02:30
  • @Cairnarvon Nope. This does not have to do with networking or seeing how long it has been run, I want to make sure that the updating of images is not faster on other systems or vice versa. –  Jan 16 '15 at 02:43

1 Answers1

0

If you Use System.nanoTime() it should work

This method can only be used to measure elapsed time and You need to divide by 1 million to get the elapsed milliseconds.

  • So far this is working, is there a way to slow it down if needed? I am using the code snippet Main.creeperX += 1 * Main.getDelta() and it goes kind of fast? Any way to slow it down? –  Jan 16 '15 at 02:45
  • Also, the delta seems to go higher as when the x is printed it goes from normal as 1000-2000 in a second (when not slowed down) to 10,000-70,000 or it goes so fast the console lags when printing it. –  Jan 16 '15 at 03:06