Phaser.Button = function (game, x, y, key, callback, callbackContext, overFrame, outFrame, downFrame, upFrame){ x = x || 0; y = y || 0; key = key || null ; callback = callback || null ; callbackContext = callbackContext || this; Phaser.Image.call(this, game, x, y, key, outFrame); this.type = Phaser.BUTTON; this._onOverFrameName = null ; this._onOutFrameName = null ; this._onDownFrameName = null ; this._onUpFrameName = null ; this._onOverFrameID = null ; this._onOutFrameID = null ; this._onDownFrameID = null ; this._onUpFrameID = null ; this.onOverMouseOnly = false ; this.onOverSound = null ; this.onOutSound = null ; this.onDownSound = null ; this.onUpSound = null ; this.onOverSoundMarker = ''; this.onOutSoundMarker = ''; this.onDownSoundMarker = ''; this.onUpSoundMarker = ''; this.onInputOver = new Phaser.Signal(); this.onInputOut = new Phaser.Signal(); this.onInputDown = new Phaser.Signal(); this.onInputUp = new Phaser.Signal(); this.freezeFrames = false ; this.forceOut = false ; this.inputEnabled = true ; this.input.start(0, true ); this.setFrames(overFrame, outFrame, downFrame, upFrame); if (callback !== null ) { this.onInputUp.add(callback, callbackContext); } this.events.onInputOver.add(this.onInputOverHandler, this); this.events.onInputOut.add(this.onInputOutHandler, this); this.events.onInputDown.add(this.onInputDownHandler, this); this.events.onInputUp.add(this.onInputUpHandler, this); } ; Phaser.Button.prototype = Object.create(Phaser.Image.prototype); Phaser.Button.prototype.constructor = Phaser.Button; Phaser.Button.prototype.clearFrames = function (){ this._onOverFrameName = null ; this._onOverFrameID = null ; this._onOutFrameName = null ; this._onOutFrameID = null ; this._onDownFrameName = null ; this._onDownFrameID = null ; this._onUpFrameName = null ; this._onUpFrameID = null ; } ; Phaser.Button.prototype.setFrames = function (overFrame, outFrame, downFrame, upFrame){ this.clearFrames(); if (overFrame !== null ) { if (typeof overFrame === 'string') { this._onOverFrameName = overFrame; if (this.input.pointerOver()) { this.frameName = overFrame; } } else { this._onOverFrameID = overFrame; if (this.input.pointerOver()) { this.frame = overFrame; } } } if (outFrame !== null ) { if (typeof outFrame === 'string') { this._onOutFrameName = outFrame; if (this.input.pointerOver() === false ) { this.frameName = outFrame; } } else { this._onOutFrameID = outFrame; if (this.input.pointerOver() === false ) { this.frame = outFrame; } } } if (downFrame !== null ) { if (typeof downFrame === 'string') { this._onDownFrameName = downFrame; if (this.input.pointerDown()) { this.frameName = downFrame; } } else { this._onDownFrameID = downFrame; if (this.input.pointerDown()) { this.frame = downFrame; } } } if (upFrame !== null ) { if (typeof upFrame === 'string') { this._onUpFrameName = upFrame; if (this.input.pointerUp()) { this.frameName = upFrame; } } else { this._onUpFrameID = upFrame; if (this.input.pointerUp()) { this.frame = upFrame; } } } } ; Phaser.Button.prototype.setSounds = function (overSound, overMarker, downSound, downMarker, outSound, outMarker, upSound, upMarker){ this.setOverSound(overSound, overMarker); this.setOutSound(outSound, outMarker); this.setDownSound(downSound, downMarker); this.setUpSound(upSound, upMarker); } ; Phaser.Button.prototype.setOverSound = function (sound, marker){ this.onOverSound = null ; this.onOverSoundMarker = ''; if (sound instanceof Phaser.Sound) { this.onOverSound = sound; } if (typeof marker === 'string') { this.onOverSoundMarker = marker; } } ; Phaser.Button.prototype.setOutSound = function (sound, marker){ this.onOutSound = null ; this.onOutSoundMarker = ''; if (sound instanceof Phaser.Sound) { this.onOutSound = sound; } if (typeof marker === 'string') { this.onOutSoundMarker = marker; } } ; Phaser.Button.prototype.setDownSound = function (sound, marker){ this.onDownSound = null ; this.onDownSoundMarker = ''; if (sound instanceof Phaser.Sound) { this.onDownSound = sound; } if (typeof marker === 'string') { this.onDownSoundMarker = marker; } } ; Phaser.Button.prototype.setUpSound = function (sound, marker){ this.onUpSound = null ; this.onUpSoundMarker = ''; if (sound instanceof Phaser.Sound) { this.onUpSound = sound; } if (typeof marker === 'string') { this.onUpSoundMarker = marker; } } ; Phaser.Button.prototype.onInputOverHandler = function (sprite, pointer){ if (this.freezeFrames === false ) { this.setState(1); } if (this.onOverMouseOnly && !pointer.isMouse) { return ; } if (this.onOverSound) { this.onOverSound.play(this.onOverSoundMarker); } if (this.onInputOver) { this.onInputOver.dispatch(this, pointer); } } ; Phaser.Button.prototype.onInputOutHandler = function (sprite, pointer){ if (this.freezeFrames === false ) { this.setState(2); } if (this.onOutSound) { this.onOutSound.play(this.onOutSoundMarker); } if (this.onInputOut) { this.onInputOut.dispatch(this, pointer); } } ; Phaser.Button.prototype.onInputDownHandler = function (sprite, pointer){ if (this.freezeFrames === false ) { this.setState(3); } if (this.onDownSound) { this.onDownSound.play(this.onDownSoundMarker); } if (this.onInputDown) { this.onInputDown.dispatch(this, pointer); } } ; Phaser.Button.prototype.onInputUpHandler = function (sprite, pointer, isOver){ if (this.onUpSound) { this.onUpSound.play(this.onUpSoundMarker); } if (this.onInputUp) { this.onInputUp.dispatch(this, pointer, isOver); } if (this.freezeFrames) { return ; } if (this.forceOut) { this.setState(2); } else { if (this._onUpFrameName !== null || this._onUpFrameID !== null ) { this.setState(4); } else { if (isOver) { this.setState(1); } else { this.setState(2); } } } } ; Phaser.Button.prototype.setState = function (newState){ if (newState === 1) { if (this._onOverFrameName != null ) { this.frameName = this._onOverFrameName; } else if (this._onOverFrameID != null ) { this.frame = this._onOverFrameID; } } else if (newState === 2) { if (this._onOutFrameName != null ) { this.frameName = this._onOutFrameName; } else if (this._onOutFrameID != null ) { this.frame = this._onOutFrameID; } } else if (newState === 3) { if (this._onDownFrameName != null ) { this.frameName = this._onDownFrameName; } else if (this._onDownFrameID != null ) { this.frame = this._onDownFrameID; } } else if (newState === 4) { if (this._onUpFrameName != null ) { this.frameName = this._onUpFrameName; } else if (this._onUpFrameID != null ) { this.frame = this._onUpFrameID; } } } ;