我已經使用這個教程來幫助我爲我的作業寫一個簡單的JS遊戲。然而,我現在正在研究gameloop,我不知道這個特定功能是如何工作的。date.now()'then'在這裏做什麼? - Javascript
這是本教程的URL。的代碼,你要查找的塊是在8 「的遊戲主循環」 http://www.lostdecadegames.com/how-to-make-a-simple-html5-canvas-game/
//Gameloop
var main = function() {
//var with timestamp
var now = Date.now();
//where does 'then' come from? I never declared it.
var delta = now - then;
//next up it just calls another func and provides parameter delta divided by 1000 which is the amount of miliseconds in a second
update(delta/1000);
//and it calls my render function
render();
//then it sets then back to Date.now()
then = now;
//No idea what this line does. still looking into it
requestAnimationFrame(main);
};
我投票作爲題外話,因爲它是要求找到一些示例源代碼 – rlemon
'then'在一個層次較高的範圍,無疑宣佈的聲明,關閉了這個問題 - 最有可能 - 主關閉。表達式函數變量'main'用於(無疑)更新調用之間的時間間隔並呈現相應的幀;將'then'更新爲'now',使其爲隨後的調用相同表達式(main)的'requestAnimationFrame'做好準備,以進行新的更新和delta計算。 –