我最近開始工作了一個JavaScript俄羅斯方塊模板,我試圖實現一個暫停功能卡住了。我正在嘗試創建一個window.onkeydown函數,以便當單擊一個按鈕時,遊戲將暫停並且一旦再次單擊就會切換爲繼續。Javascript:暫停/暫停JavaScript俄羅斯方塊遊戲
這裏是我的代碼片段
function get(id) {
return document.getElementById(id);
};
function hide(id) {
get(id).style.visibility = 'hidden';
};
function show(id) {
get(id).style.visibility = null;
};
function html(id, html) {
get(id).innerHTML = html;
};
function timestamp() {
return new Date().getTime();
};
function random(min, max) {
return (min + (Math.random() * (max - min)));
};
function randomChoice(choices) {
return choices[Math.round(random(0, choices.length - 1))];
};
if (!window.requestAnimationFrame) { // http://paulirish.com/2011/requestanimationframe-for-smart-animating/
window.requestAnimationFrame = window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback, element) {
window.setTimeout(callback, 1000/60);
}
}
var isPaused = true;
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000/60);
};
})();
function Start() {
if (isPaused) {
Update();
}
requestAnimFrame(Start);
}
window.onkeydown = function() {
isPaused = !isPaused; // flips the pause state
};
任何人都可以點我在正確的方向?
爲什麼你有兩個請求動畫幀的實現? –
暫停JavaScript執行,直到按下按鈕:http://stackoverflow.com/a/10366406/2618541 – Panayotis