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.physicsType = Phaser.SPRITE; this._onOverFrame = null ; this._onOutFrame = null ; this._onDownFrame = null ; this._onUpFrame = null ; 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.onOverMouseOnly = true ; this.justReleasedPreventsOver = Phaser.PointerMode.TOUCH; this.freezeFrames = false ; this.forceOut = false ; this.inputEnabled = true ; this.input.start(0, true ); this.input.useHandCursor = 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); this.events.onRemovedFromWorld.add(this.removedFromWorld, this); } ; Phaser.Button.prototype = Object.create(Phaser.Image.prototype); Phaser.Button.prototype.constructor = Phaser.Button; var STATE_OVER = 'Over'; var STATE_OUT = 'Out'; var STATE_DOWN = 'Down'; var STATE_UP = 'Up'; Phaser.Button.prototype.clearFrames = function (){ this.setFrames(null , null , null , null ); } ; Phaser.Button.prototype.removedFromWorld = function (){ this.inputEnabled = false ; } ; Phaser.Button.prototype.setStateFrame = function (state, frame, switchImmediately){ var frameKey = '_on' + state + 'Frame'; if (frame !== null ) { this[frameKey] = frame; if (switchImmediately) { this.changeStateFrame(state); } } else { this[frameKey] = null ; } } ; Phaser.Button.prototype.changeStateFrame = function (state){ if (this.freezeFrames) { return false ; } var frameKey = '_on' + state + 'Frame'; var frame = this[frameKey]; if (typeof frame === 'string') { this.frameName = frame; return true ; } else if (typeof frame === 'number') { this.frame = frame; return true ; } else { return false ; } } ; Phaser.Button.prototype.setFrames = function (overFrame, outFrame, downFrame, upFrame){ this.setStateFrame(STATE_OVER, overFrame, this.input.pointerOver()); this.setStateFrame(STATE_OUT, outFrame, !this.input.pointerOver()); this.setStateFrame(STATE_DOWN, downFrame, this.input.pointerDown()); this.setStateFrame(STATE_UP, upFrame, this.input.pointerUp()); } ; Phaser.Button.prototype.setStateSound = function (state, sound, marker){ var soundKey = 'on' + state + 'Sound'; var markerKey = 'on' + state + 'SoundMarker'; if (sound instanceof Phaser.Sound || sound instanceof Phaser.AudioSprite) { this[soundKey] = sound; this[markerKey] = typeof marker === 'string'? marker: ''; } else { this[soundKey] = null ; this[markerKey] = ''; } } ; Phaser.Button.prototype.playStateSound = function (state){ var soundKey = 'on' + state + 'Sound'; var sound = this[soundKey]; if (sound) { var markerKey = 'on' + state + 'SoundMarker'; var marker = this[markerKey]; sound.play(marker); return true ; } else { return false ; } } ; Phaser.Button.prototype.setSounds = function (overSound, overMarker, downSound, downMarker, outSound, outMarker, upSound, upMarker){ this.setStateSound(STATE_OVER, overSound, overMarker); this.setStateSound(STATE_OUT, outSound, outMarker); this.setStateSound(STATE_DOWN, downSound, downMarker); this.setStateSound(STATE_UP, upSound, upMarker); } ; Phaser.Button.prototype.setOverSound = function (sound, marker){ this.setStateSound(STATE_OVER, sound, marker); } ; Phaser.Button.prototype.setOutSound = function (sound, marker){ this.setStateSound(STATE_OUT, sound, marker); } ; Phaser.Button.prototype.setDownSound = function (sound, marker){ this.setStateSound(STATE_DOWN, sound, marker); } ; Phaser.Button.prototype.setUpSound = function (sound, marker){ this.setStateSound(STATE_UP, sound, marker); } ; Phaser.Button.prototype.onInputOverHandler = function (sprite, pointer){ if (pointer.justReleased() && (this.justReleasedPreventsOver & pointer.pointerMode) === pointer.pointerMode) { return ; } this.changeStateFrame(STATE_OVER); if (this.onOverMouseOnly && !pointer.isMouse) { return ; } this.playStateSound(STATE_OVER); if (this.onInputOver) { this.onInputOver.dispatch(this, pointer); } } ; Phaser.Button.prototype.onInputOutHandler = function (sprite, pointer){ this.changeStateFrame(STATE_OUT); this.playStateSound(STATE_OUT); if (this.onInputOut) { this.onInputOut.dispatch(this, pointer); } } ; Phaser.Button.prototype.onInputDownHandler = function (sprite, pointer){ this.changeStateFrame(STATE_DOWN); this.playStateSound(STATE_DOWN); if (this.onInputDown) { this.onInputDown.dispatch(this, pointer); } } ; Phaser.Button.prototype.onInputUpHandler = function (sprite, pointer, isOver){ this.playStateSound(STATE_UP); if (this.onInputUp) { this.onInputUp.dispatch(this, pointer, isOver); } if (this.freezeFrames) { return ; } if (this.forceOut === true || (this.forceOut & pointer.pointerMode) === pointer.pointerMode) { this.changeStateFrame(STATE_OUT); } else { var changedUp = this.changeStateFrame(STATE_UP); if (!changedUp) { if (isOver) { this.changeStateFrame(STATE_OVER); } else { this.changeStateFrame(STATE_OUT); } } } } ;