我需要为小游戏实现毫秒精确的计时器.哪种方法最合适呢?我目前的方法是使用System.currentTimeMillis().它似乎工作得很好,但也许有一些我不知道的陷阱.什么是计时的标准方法?
您的计时器只会与您的系统实现它一样好.它根本不会比那更好.
作为一名前职业游戏开发人员(并且仍在做侧面项目),我建议不要达到毫秒精度要求.你可能不需要它.
使用此程序确定您的时钟准确度:
/*
C:\junk>javac TimerTest.java
C:\junk>java TimerTest
Your delay is : 16 millis
C:\junk>
*/
class TimerTest {
public static void main(String[] args) {
long then = System.currentTimeMillis();
long now = then;
// this loop just spins until the time changes.
// when it changes, we will see how accurate it is
while((now = System.currentTimeMillis()) == then); // busy wait
System.out.println("Your delay is : " + (now-then) + " millis");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
586 次 |
| 最近记录: |