打印deltaTime 時(shí) 刷新幀的時(shí)間顯示為NaN,不知道什么原因。。。
function game(){
? ?init();
? ?lastTime = Date.now();
? ?deltaTime = 0;
? ?gameloop();
}
function init(){
? ?//獲取canvas context
? ?can1 = document.getElementById("canvas1"); //第一層 小魚(yú)
? ?ctx1 = can1.getContext('2d');
? ?can2 = document.getElementById("canvas2"); //第二層 背景 & ???br /> ? ?ctx2 = can2.getContext('2d');
? ?bgPic.src="./src/background.jpg";
? ?canWidth=can1.width;
? ?canHeight=can1.height;
}
function gameloop(){
? ?//游戲循環(huán) 相對(duì)于setInterval, setTimeout
? window.requestAnimFrame (gameloop); // 當(dāng)前繪制完成后根據(jù)機(jī)器性能計(jì)算多久執(zhí)行一幀函數(shù),會(huì)有動(dòng)態(tài)的時(shí)間間隔
? ?var now = Date.now;
? ?deltaTime = now - lastTime;
? ?lastTime = now;
? ?console.log(deltaTime);
打印后顯示為:
? ?drawBackground();
2016-03-05
Date.now(). 只寫(xiě)Date.now是個(gè)function