0

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);
 };
 ...
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • By chance, are you reverting filesLoaded to 0 at some point in our progress function? After everything is loaded, do you reset it to 0, but forget to reset filesToLoad to 0 as well? It might prevent the over event from being emitted again – Cory Danielson Feb 11 '15 at 03:59
  • no i reset everything everytime i use load function and if i add a console log in the else statement, i see it. The problem is really about the emit i think. :/ – Quentin Le Bévillon Feb 11 '15 at 04:24

0 Answers0