开发者

Problem with System.nanoTime()

I´m working on a 2d java game but I´m stuck on timer problems. Here is my game loop:

void gameLoop(isRunning){
....

doStuff();

....
}

I have a fps measuring code like this inside the loop:

long thisLoop = System.currentTimeMillis();
delta = thisLoop - lastLoopTime;
lastLoopTime = thisLoop;

So I get how much time has passed since last loop. However, whenever I try to use System.nanoTime() instead of System,currentTimeMillis() like this:

long thisLoop = System.nanoTime()开发者_StackOverflow中文版;
delta = thisLoop - lastLoopTime;
lastLoopTime = thisLoop;

My game gets completely screwed, doesn't render anything past first frame, no errors reported just frozen. I´m on win 7 64 lastest java 1.6. What could be wrong?


Try using System.nanoTime() / 1000000 since it's in nanoseconds instead of milliseconds like you're probably expecting.


Are you multiplying or dividing by 1,000,000? 1 millisecond = 1 000 000 nanoseconds. I'm assuming that the rest of your logic is implemented in terms of milliseconds.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜