From bbddcbcfa835b91e6ebb38646a2f27f2ae576605 Mon Sep 17 00:00:00 2001 From: dethe Date: Fri, 25 Sep 2015 22:58:58 -0700 Subject: [PATCH] clear all animationFrameHandlers --- js/runtime.js | 21 ++++++++++++++++----- js/util.js | 5 +++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/js/runtime.js b/js/runtime.js index e2bd3d20..42da237e 100644 --- a/js/runtime.js +++ b/js/runtime.js @@ -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. */ @@ -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(){ @@ -109,7 +116,9 @@ perFrameHandlers.forEach(function(handler){ handler(); }); - requestAnimationFrame(frameHandler); + if (perFrameHandlers.length){ + requestAnimationFrame(frameHandler); + } } @@ -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){ @@ -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; } } }; diff --git a/js/util.js b/js/util.js index f536307b..6421d58d 100644 --- a/js/util.js +++ b/js/util.js @@ -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