I am working on an assets loader, this class inherits EventEmitter
for progress and over events.
I use it this way:
// LOAD INITIAL ASSETS & START GAME AFTER
assets.on('over',function() {
// START GAME
HOB.start();
});
assets.load({'loadingBackground' : '/img/loadingBackground.png'});
This is working great but ....
On my load function when loading is over. I use removeAllListeners
.
var progress = function() {
if(filesLoaded < filesToLoad) {
...
} else {
...
that.emit('over',filesLoaded);
if(removeEvents)
that.removeAllListeners();
}
}
So the events are removed OKAY, but I cannot re-add over event progress, the second time its not working :/
// LOAD ASSETS AND MAP & SET PLAY STATE AFTER
assets.on('over',function () {
// LOAD BASE MAP & SET STATE
...
currentState = PlayState
});
console.log(assets._events); // -> return empty ({})
assets.load({
'001-Grassland01' : 'img/tilesets/001-Grassland01.png',
'002-Woods01' : 'img/tilesets/002-Woods01.png',
'015-ForestTown01' : 'img/tilesets/015-ForestTown01.png'
});
|-> Not working.
An interesting thing to know is that the _events property is empty after the second .on('over')
.
Do you have any ideas?
EDIT
I found a way overriding the on
method.
var AssetsManager = function () {
...
this.on = function(event,func) {
if(this._events[event])
delete this._events[event];
return this.__proto__.on.call(this,event,func);
};
...
}