Skip to content
Permalink
Browse files

sprite: fix memory leak

  • Loading branch information
andrewrk committed Jul 27, 2013
1 parent bdb959c commit 900ba7c6f5c27617d9917653ee4d63e4f043374d
Showing with 25 additions and 15 deletions.
  1. +25 −15 lib/sprite.js
@@ -18,6 +18,7 @@ function Sprite(animationName, params) {
this.rotation = params.rotation == null ? 0 : params.rotation;
this.alpha = params.alpha == null ? 1 : params.alpha;

setUpListeners(this);
this.setAnimationName(animationName);
this.setLoop(params.loop);
this.setVisible(params.visible == null ? true : params.visible);
@@ -177,12 +178,16 @@ Sprite.prototype.getFrameIndex = function(){
};

Sprite.prototype['delete'] = function(){
if (this.interval) this.interval();
this.removeAllListeners();
if (this.batch) this.batch.remove(this);
this.batch = null;
};

function setUpInterval(self) {
if (self.interval) self.interval.cancel();
if (self.interval) self.interval();
self.interval = null;
if (self.animationEndListenCount === 0) return;
var _schedule = self._loop ? schedule : wait;
var now = new Date();
var timeSinceStart = (now - self.animationStartDate) / 1000;
@@ -194,24 +199,29 @@ function setUpInterval(self) {

function wait(sec, cb) {
var interval = setTimeout(cb, sec * 1000);
return {
cancel: function(){
if (interval != null) {
clearTimeout(interval);
interval = null;
}
}
return function(){
clearTimeout(interval);
};
}

function schedule(sec, cb) {
var interval = setInterval(cb, sec * 1000);
return {
cancel: function(){
if (interval != null) {
clearInterval(interval);
interval = null;
}
}
return function(){
clearInterval(interval);
};
}

function setUpListeners(self) {
self.on('newListener', function(event) {
if (event === 'animationend') {
self.animationEndListenCount += 1;
setUpInterval(self);
}
});
self.on('removeListener', function(event) {
if (event === 'animationend') {
self.animationEndListenCount -= 1;
setUpInterval(self);
}
});
}

0 comments on commit 900ba7c

Please sign in to comment.
You can’t perform that action at this time.