Skip to content

Commit

Permalink
clear all animationFrameHandlers
Browse files Browse the repository at this point in the history
  • Loading branch information
dethe committed Sep 26, 2015
1 parent 6afc646 commit bbddcbc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
21 changes: 16 additions & 5 deletions js/runtime.js
Expand Up @@ -70,6 +70,7 @@
function clearRuntime() {
/* FIXME: Event.clearRuntime() should be moved to runtime.js.
* See: https://github.com/waterbearlang/waterbear/issues/968 */
// console.log('clearing runtime');
Event.clearRuntime();
clearPerFrameHandlers();
/* Clear all runtime event handlers. */
Expand All @@ -78,26 +79,32 @@

var perFrameHandlers;
var lastTime;
var animationFrameHandler;

function clearPerFrameHandlers() {
perFrameHandlers = [];
lastTime = new Date().valueOf();
if (animationFrameHandler){
cancelAnimationFrame(animationFrameHandler);
animationFrameHandler = null;
}
}

// Initialize the stage.
Event.on(window, 'ui:resize', null, handleResize);
Event.on(document.body, 'ui:wb-resize', null, handleResize);


function startEventLoop(){
// console.log('start event loop');
clearPerFrameHandlers();
runtime.control._frame = 0;
runtime.control._sinceLastTick = 0;
requestAnimationFrame(frameHandler);
animationFrameHandler = requestAnimationFrame(frameHandler);
}

function stopEventLoop() {
/* TODO: Dunno lol there be more in here? */
// console.log('stop event loop');
}

function frameHandler(){
Expand All @@ -109,7 +116,9 @@
perFrameHandlers.forEach(function(handler){
handler();
});
requestAnimationFrame(frameHandler);
if (perFrameHandlers.length){
requestAnimationFrame(frameHandler);
}
}


Expand Down Expand Up @@ -795,7 +804,7 @@
return assets.sounds[url]; // already cached by sounds library
},
play: function(sound){
console.log('sound.play()');
// console.log('sound.play()');
sound.play();
},
setLoop: function(sound, flag){
Expand Down Expand Up @@ -1084,7 +1093,9 @@
return vec.y;
},
randomUnitVector: function randomUnitVector(){
return util.Vector.fromPolar(util.randInt(0,359), 1);
var vec = util.Vector.fromPolar(Math.random() * 360, 1);
console.log('vector magnitude: %s', vec.magnitude());
return vec;
}
}
};
Expand Down
5 changes: 5 additions & 0 deletions js/util.js
Expand Up @@ -50,6 +50,11 @@
window.webkitRequestAnimationFrame ||
function(fn){ setTimeout(fn, 20); };

window.cancelAnimationFrame = window.cancelAnimationFrame ||
window.mozCancelAnimationFrame ||
window.msCancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
function(timer){ clearTimeout(timer); };

// add defaultValue if key does't exist in an object yet and return it
// otherwise return current valud of key
Expand Down

0 comments on commit bbddcbc

Please sign in to comment.