html - Javascript: Pause/Unpause javascript Tetris game -
i have started working off javascript tetris template , i've got stuck trying implement pause feature. trying create window.onkeydown function when button clicked game pause , toggle continue once clicked again.
here snippet of code
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 pause state };
can point me in right direction?
you have keep reference on settimeout. like,
module.timeout = settimeout(callback, 1000/60);
so when flip ispaused, clear both timeouts , restart them later when you're pausing them.
cleartimeout(module.timeout)
Comments
Post a Comment