Phaser.Signal = function (){ this._bindings = [] ; this._prevParams = null ; var self = this; this.dispatch = function (){ Phaser.Signal.prototype.dispatch.apply(self, arguments); } ; } ; Phaser.Signal.prototype = { memorize: false , _shouldPropagate: true , active: true , validateListener: function (listener, fnName){ if (typeof listener !== 'function') { throw new Error((_AN_Call_replace('replace', 'Phaser.Signal: listener is a required param of {fn}() and should be a Function.', '{fn}', fnName))) } } , _registerListener: function (listener, isOnce, listenerContext, priority){ var prevIndex = this._indexOfListener(listener, listenerContext); var binding; if (prevIndex !== -1) { binding = this._bindings[prevIndex]; if (binding.isOnce() !== isOnce) { throw new Error('You cannot add' + (isOnce? '': 'Once') + '() then add' + (!isOnce? '': 'Once') + '() the same listener without removing the relationship first.') } } else { binding = new Phaser.SignalBinding(this, listener, isOnce, listenerContext, priority); this._addBinding(binding); } if (this.memorize && this._prevParams) { binding.execute(this._prevParams); } return binding; } , _addBinding: function (binding){ var n = _AN_Read_length('length', this._bindings); do { n-- ; } while(this._bindings[n] && binding._priority <= this._bindings[n]._priority)this._bindings.splice(n + 1, 0, binding); } , _indexOfListener: function (listener, context){ var n = _AN_Read_length('length', this._bindings); var cur; while (n-- ){ cur = this._bindings[n]; if (cur._listener === listener && cur.context === context) { return n; } } return -1; } , has: function (listener, context){ return this._indexOfListener(listener, context) !== -1; } , add: function (listener, listenerContext, priority){ this.validateListener(listener, 'add'); return this._registerListener(listener, false , listenerContext, priority); } , addOnce: function (listener, listenerContext, priority){ this.validateListener(listener, 'addOnce'); return this._registerListener(listener, true , listenerContext, priority); } , remove: function (listener, context){ this.validateListener(listener, 'remove'); var i = this._indexOfListener(listener, context); if (i !== -1) { this._bindings[i]._destroy(); this._bindings.splice(i, 1); } return listener; } , removeAll: function (context){ if (typeof context === 'undefined') { context = null ; } var n = _AN_Read_length('length', this._bindings); while (n-- ){ if (context) { if (this._bindings[n].context === context) { this._bindings[n]._destroy(); this._bindings.splice(n, 1); } } else { this._bindings[n]._destroy(); } } if (!context) { this._bindings.length = 0; } } , getNumListeners: function (){ return _AN_Read_length('length', this._bindings); } , halt: function (){ this._shouldPropagate = false ; } , dispatch: function (){ if (!this.active) { return ; } var paramsArr = Array.prototype.slice.call(arguments); var n = _AN_Read_length('length', this._bindings); var bindings; if (this.memorize) { this._prevParams = paramsArr; } if (!n) { return ; } bindings = this._bindings.slice(); this._shouldPropagate = true ; do { n-- ; } while(bindings[n] && this._shouldPropagate && bindings[n].execute(paramsArr) !== false )} , forget: function (){ this._prevParams = null ; } , dispose: function (){ this.removeAll(); delete this._bindings; delete this._prevParams; } , toString: function (){ return '[Phaser.Signal active:' + this.active + ' numListeners:' + this.getNumListeners() + ']'; } } ; Phaser.Signal.prototype.constructor = Phaser.Signal;