我正在學習一個教程,下面是生成邏輯和框架更新的運行方法。我瞭解蜱是如何更新60次/秒,但我不明白我們如何每秒調整幀數。Java - Thread.sleep內部運行方法
現在使用Thread.sleep(2),每秒幀數大約是460.沒有它,數字就會增加,每秒大約有1000萬次更新。代碼Thread.sleep(2)將線程掛起僅2毫秒吧? Thread.sleep爲什麼/如何在這裏工作以降低它?
創建一個nsPerFrame = 1000000000D /(FPS)D來設置任何FPS我想要的方式,他做蜱的方式是不是更簡單?
public void run(){
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D/60D;
int frames = 0;
int ticks = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime)/nsPerTick;
lastTime = now;
while(delta >= 1){
ticks++;
tick();
delta-= 1;
}
try{
Thread.sleep(2);
} catch(InterruptedException e){
e.printStackTrace();
}
frames++;
render();
if(System.currentTimeMillis() - lastTimer >= 1000){
lastTimer += 1000;
System.out.println(ticks + "," + frames);
frames = 0;
ticks = 0;
}
}
}
謝謝,這清除了事情! – user3421485