我想暫停功能添加到遊戲循環我有一個遊戲循環:暫停使用「requestAnimationFrame()」
GameLoop = {
//Insert more members here if needed
run : function(options) {
var now,
dt = 0,
//last = timestamp(),
last = Date.now(),
slow = options.slow || 1, // slow motion scaling factor
step = 1/options.fps || 60,
slowStep = slow * step,
update = options.update,
render = options.render;
function frame() {
//now = timestamp();
now = Date.now();
dt = dt + Math.min(1, (now - last)/1000);
while(dt > slowStep) {
dt = dt - slowStep;
update(step);
}
render(dt/slow);
last = now;
requestAnimationFrame(frame, options.canvas);
}
requestAnimationFrame(frame);
}
};
我用了一些文檔和教程去這個結構而且我已經有了一款似乎與其完美搭配的遊戲。
有沒有一種方法可以添加像'暫停'這樣的功能,以便它停下來,然後我可以在任何時候恢復並繼續正常運行?
讓我知道如果你需要更多的代碼或任何額外的iformation,謝謝!
在遊戲對象中添加一個可以翻轉的bool變量,稱爲'isRunning'。當「isRunning」爲false時,請勿在幀函數中運行內容。 – teynon
暫停遊戲通常不是通過停止渲染循環來完成的,而是通過跳過循環內的模擬步驟來完成。許多遊戲甚至在暫停屏幕上都有動畫。 –
另外,爲什麼你在'requestAnimationFrame'上有第二個參數? –