2013-09-19 11:17:49 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2015-02-25 03:36:23 +00:00
|
|
|
* @copyright 2015 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
2013-09-19 11:17:49 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-08 23:30:44 +00:00
|
|
|
/**
|
2014-10-29 06:26:35 +00:00
|
|
|
* Create a new `Button` object. A Button is a special type of Sprite that is set-up to handle Pointer events automatically.
|
|
|
|
*
|
|
|
|
* The four states a Button responds to are:
|
2013-11-28 15:57:09 +00:00
|
|
|
*
|
|
|
|
* * 'Over' - when the Pointer moves over the Button. This is also commonly known as 'hover'.
|
|
|
|
* * 'Out' - when the Pointer that was previously over the Button moves out of it.
|
|
|
|
* * 'Down' - when the Pointer is pressed down on the Button. I.e. touched on a touch enabled device or clicked with the mouse.
|
|
|
|
* * 'Up' - when the Pointer that was pressed down on the Button is released again.
|
|
|
|
*
|
2014-10-29 06:26:35 +00:00
|
|
|
* A different texture/frame and activation sound can be specified for any of the states.
|
|
|
|
*
|
|
|
|
* Frames can be specified as either an integer (the frame ID) or a string (the frame name); the same values that can be used with a Sprite constructor.
|
2013-10-30 03:46:52 +00:00
|
|
|
*
|
2014-09-16 16:35:08 +00:00
|
|
|
* @class Phaser.Button
|
2013-09-16 14:37:30 +00:00
|
|
|
* @constructor
|
2014-07-11 17:02:40 +00:00
|
|
|
* @extends Phaser.Image
|
2013-09-19 14:34:48 +00:00
|
|
|
* @param {Phaser.Game} game Current game instance.
|
2013-12-31 17:03:09 +00:00
|
|
|
* @param {number} [x=0] - X position of the Button.
|
|
|
|
* @param {number} [y=0] - Y position of the Button.
|
2014-10-29 06:26:35 +00:00
|
|
|
* @param {string} [key] - The image key (in the Game.Cache) to use as the texture for this Button.
|
2013-10-30 03:46:52 +00:00
|
|
|
* @param {function} [callback] - The function to call when this Button is pressed.
|
|
|
|
* @param {object} [callbackContext] - The context in which the callback will be called (usually 'this').
|
2014-10-29 06:26:35 +00:00
|
|
|
* @param {string|integer} [overFrame] - The frame / frameName when the button is in the Over state.
|
|
|
|
* @param {string|integer} [outFrame] - The frame / frameName when the button is in the Out state.
|
|
|
|
* @param {string|integer} [downFrame] - The frame / frameName when the button is in the Down state.
|
|
|
|
* @param {string|integer} [upFrame] - The frame / frameName when the button is in the Up state.
|
2013-09-08 23:30:44 +00:00
|
|
|
*/
|
2013-12-31 17:03:09 +00:00
|
|
|
Phaser.Button = function (game, x, y, key, callback, callbackContext, overFrame, outFrame, downFrame, upFrame) {
|
2013-09-08 23:30:44 +00:00
|
|
|
|
|
|
|
x = x || 0;
|
|
|
|
y = y || 0;
|
|
|
|
key = key || null;
|
|
|
|
callback = callback || null;
|
|
|
|
callbackContext = callbackContext || this;
|
|
|
|
|
2014-07-11 17:02:40 +00:00
|
|
|
Phaser.Image.call(this, game, x, y, key, outFrame);
|
2013-09-08 23:30:44 +00:00
|
|
|
|
2014-03-23 07:59:28 +00:00
|
|
|
/**
|
2014-10-29 06:26:35 +00:00
|
|
|
* The Phaser Object Type.
|
|
|
|
* @property {number} type
|
2014-10-30 02:31:03 +00:00
|
|
|
* @readonly
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2013-09-12 20:54:41 +00:00
|
|
|
this.type = Phaser.BUTTON;
|
|
|
|
|
2015-03-23 15:04:27 +00:00
|
|
|
/**
|
|
|
|
* @property {number} physicsType - The const physics body type of this object.
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
this.physicsType = Phaser.SPRITE;
|
|
|
|
|
2014-03-23 07:59:28 +00:00
|
|
|
/**
|
2014-10-30 02:31:03 +00:00
|
|
|
* The name or ID of the Over state frame.
|
|
|
|
* @property {string|integer} onOverFrame
|
2013-11-25 03:13:04 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-10-30 02:31:03 +00:00
|
|
|
this._onOverFrame = null;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-30 02:31:03 +00:00
|
|
|
* The name or ID of the Out state frame.
|
|
|
|
* @property {string|integer} onOutFrame
|
2013-11-25 03:13:04 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-10-30 02:31:03 +00:00
|
|
|
this._onOutFrame = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name or ID of the Down state frame.
|
|
|
|
* @property {string|integer} onDownFrame
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._onDownFrame = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name or ID of the Up state frame.
|
|
|
|
* @property {string|integer} onUpFrame
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._onUpFrame = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Sound to be played when this Buttons Over state is activated.
|
|
|
|
* @property {Phaser.Sound|Phaser.AudioSprite|null} onOverSound
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
this.onOverSound = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Sound to be played when this Buttons Out state is activated.
|
|
|
|
* @property {Phaser.Sound|Phaser.AudioSprite|null} onOutSound
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
this.onOutSound = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Sound to be played when this Buttons Down state is activated.
|
|
|
|
* @property {Phaser.Sound|Phaser.AudioSprite|null} onDownSound
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
this.onDownSound = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Sound to be played when this Buttons Up state is activated.
|
|
|
|
* @property {Phaser.Sound|Phaser.AudioSprite|null} onUpSound
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
this.onUpSound = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Sound Marker used in conjunction with the onOverSound.
|
|
|
|
* @property {string} onOverSoundMarker
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
this.onOverSoundMarker = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Sound Marker used in conjunction with the onOutSound.
|
|
|
|
* @property {string} onOutSoundMarker
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
this.onOutSoundMarker = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Sound Marker used in conjunction with the onDownSound.
|
|
|
|
* @property {string} onDownSoundMarker
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
this.onDownSoundMarker = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Sound Marker used in conjunction with the onUpSound.
|
|
|
|
* @property {string} onUpSoundMarker
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
this.onUpSoundMarker = '';
|
2014-03-23 07:59:28 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-29 06:26:35 +00:00
|
|
|
* The Signal (or event) dispatched when this Button is in an Over state.
|
|
|
|
* @property {Phaser.Signal} onInputOver
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-10-29 06:26:35 +00:00
|
|
|
this.onInputOver = new Phaser.Signal();
|
2014-03-23 07:59:28 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-29 06:26:35 +00:00
|
|
|
* The Signal (or event) dispatched when this Button is in an Out state.
|
|
|
|
* @property {Phaser.Signal} onInputOut
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-10-29 06:26:35 +00:00
|
|
|
this.onInputOut = new Phaser.Signal();
|
2014-03-23 07:59:28 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-29 06:26:35 +00:00
|
|
|
* The Signal (or event) dispatched when this Button is in an Down state.
|
|
|
|
* @property {Phaser.Signal} onInputDown
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-10-29 06:26:35 +00:00
|
|
|
this.onInputDown = new Phaser.Signal();
|
2013-12-31 17:03:09 +00:00
|
|
|
|
2014-03-23 07:59:28 +00:00
|
|
|
/**
|
2014-10-29 06:26:35 +00:00
|
|
|
* The Signal (or event) dispatched when this Button is in an Up state.
|
|
|
|
* @property {Phaser.Signal} onInputUp
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-10-29 06:26:35 +00:00
|
|
|
this.onInputUp = new Phaser.Signal();
|
2013-09-08 23:30:44 +00:00
|
|
|
|
2014-05-13 10:04:39 +00:00
|
|
|
/**
|
2014-10-29 06:26:35 +00:00
|
|
|
* If true then onOver events (such as onOverSound) will only be triggered if the Pointer object causing them was the Mouse Pointer.
|
|
|
|
* The frame will still be changed as applicable.
|
|
|
|
* @property {boolean} onOverMouseOnly
|
2014-05-13 10:04:39 +00:00
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.onOverMouseOnly = false;
|
2014-10-29 06:26:35 +00:00
|
|
|
|
2014-03-23 07:59:28 +00:00
|
|
|
/**
|
2014-10-29 06:26:35 +00:00
|
|
|
* When true the the texture frame will not be automatically switched on up/down/over/out events.
|
|
|
|
* @property {boolean} freezeFrames
|
2013-10-30 03:46:52 +00:00
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.freezeFrames = false;
|
|
|
|
|
2013-11-04 20:43:59 +00:00
|
|
|
/**
|
2013-12-31 17:03:09 +00:00
|
|
|
* When the Button is touched / clicked and then released you can force it to enter a state of "out" instead of "up".
|
2013-11-04 20:43:59 +00:00
|
|
|
* @property {boolean} forceOut
|
|
|
|
* @default
|
|
|
|
*/
|
2013-12-31 17:03:09 +00:00
|
|
|
this.forceOut = false;
|
2013-11-04 20:43:59 +00:00
|
|
|
|
2014-02-07 18:55:29 +00:00
|
|
|
this.inputEnabled = true;
|
|
|
|
|
|
|
|
this.input.start(0, true);
|
|
|
|
|
2015-06-17 00:45:41 +00:00
|
|
|
this.input.useHandCursor = true;
|
|
|
|
|
2013-12-31 17:03:09 +00:00
|
|
|
this.setFrames(overFrame, outFrame, downFrame, upFrame);
|
2013-09-08 23:30:44 +00:00
|
|
|
|
2013-09-09 09:30:01 +00:00
|
|
|
if (callback !== null)
|
2013-09-08 23:30:44 +00:00
|
|
|
{
|
|
|
|
this.onInputUp.add(callback, callbackContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect the input events to here so we can handle animation updates, etc
|
|
|
|
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);
|
|
|
|
|
2014-10-28 01:49:14 +00:00
|
|
|
this.events.onRemovedFromWorld.add(this.removedFromWorld, this);
|
|
|
|
|
2013-09-08 23:30:44 +00:00
|
|
|
};
|
|
|
|
|
2014-07-11 17:02:40 +00:00
|
|
|
Phaser.Button.prototype = Object.create(Phaser.Image.prototype);
|
2013-09-08 23:30:44 +00:00
|
|
|
Phaser.Button.prototype.constructor = Phaser.Button;
|
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
// State constants; local only. These are tied to property names in Phaser.Button.
|
|
|
|
var STATE_OVER = 'Over';
|
|
|
|
var STATE_OUT = 'Out';
|
|
|
|
var STATE_DOWN = 'Down';
|
|
|
|
var STATE_UP = 'Up';
|
|
|
|
|
2013-09-16 14:37:30 +00:00
|
|
|
/**
|
2013-12-31 17:03:09 +00:00
|
|
|
* Clears all of the frames set on this Button.
|
|
|
|
*
|
2014-10-29 06:26:35 +00:00
|
|
|
* @method Phaser.Button#clearFrames
|
2013-12-31 17:03:09 +00:00
|
|
|
*/
|
|
|
|
Phaser.Button.prototype.clearFrames = function () {
|
|
|
|
|
2014-10-29 06:26:35 +00:00
|
|
|
this.setFrames(null, null, null, null);
|
2013-12-31 17:03:09 +00:00
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2013-12-31 17:03:09 +00:00
|
|
|
|
2014-10-28 01:49:14 +00:00
|
|
|
/**
|
|
|
|
* Called when this Button is removed from the World.
|
|
|
|
*
|
2014-10-29 06:26:35 +00:00
|
|
|
* @method Phaser.Button#removedFromWorld
|
|
|
|
* @protected
|
2014-10-28 01:49:14 +00:00
|
|
|
*/
|
|
|
|
Phaser.Button.prototype.removedFromWorld = function () {
|
|
|
|
|
|
|
|
this.inputEnabled = false;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-12-31 17:03:09 +00:00
|
|
|
/**
|
2014-10-29 06:26:35 +00:00
|
|
|
* Set the frame name/ID for the given state.
|
2013-09-16 14:37:30 +00:00
|
|
|
*
|
2014-10-29 06:26:35 +00:00
|
|
|
* @method Phaser.Button#setStateFrame
|
|
|
|
* @private
|
2014-10-30 02:31:03 +00:00
|
|
|
* @param {object} state - See `STATE_*`
|
2014-10-29 06:26:35 +00:00
|
|
|
* @param {number|string} frame - The number or string representing the frame.
|
|
|
|
* @param {boolean} switchImmediately - Immediately switch to the frame if it was set - and this is true.
|
2013-09-16 14:37:30 +00:00
|
|
|
*/
|
2014-10-29 06:26:35 +00:00
|
|
|
Phaser.Button.prototype.setStateFrame = function (state, frame, switchImmediately)
|
|
|
|
{
|
2014-10-30 02:31:03 +00:00
|
|
|
var frameKey = '_on' + state + 'Frame';
|
|
|
|
|
2015-04-13 10:58:18 +00:00
|
|
|
if (frame !== null) // not null or undefined
|
2013-09-09 09:30:01 +00:00
|
|
|
{
|
2014-10-30 02:31:03 +00:00
|
|
|
this[frameKey] = frame;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-10-29 06:26:35 +00:00
|
|
|
if (switchImmediately)
|
2013-09-09 09:30:01 +00:00
|
|
|
{
|
2014-10-30 02:31:03 +00:00
|
|
|
this.changeStateFrame(state);
|
2013-09-09 09:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-29 06:26:35 +00:00
|
|
|
else
|
2013-09-09 09:30:01 +00:00
|
|
|
{
|
2014-10-30 02:31:03 +00:00
|
|
|
this[frameKey] = null;
|
2014-10-29 06:26:35 +00:00
|
|
|
}
|
2013-09-30 16:12:22 +00:00
|
|
|
|
2014-10-29 06:26:35 +00:00
|
|
|
};
|
2013-09-30 16:12:22 +00:00
|
|
|
|
2014-10-29 06:26:35 +00:00
|
|
|
/**
|
|
|
|
* Change the frame to that of the given state, _if_ the state has a frame assigned _and_ if the frames are not currently "frozen".
|
|
|
|
*
|
|
|
|
* @method Phaser.Button#changeStateFrame
|
|
|
|
* @private
|
2014-10-30 02:31:03 +00:00
|
|
|
* @param {object} state - See `STATE_*`
|
2014-10-29 06:26:35 +00:00
|
|
|
* @return {boolean} True only if the frame was assigned a value, possibly the same one it already had.
|
|
|
|
*/
|
|
|
|
Phaser.Button.prototype.changeStateFrame = function (state) {
|
|
|
|
|
|
|
|
if (this.freezeFrames)
|
|
|
|
{
|
|
|
|
return false;
|
2013-09-09 09:30:01 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
var frameKey = '_on' + state + 'Frame';
|
|
|
|
var frame = this[frameKey];
|
|
|
|
|
2014-10-29 06:26:35 +00:00
|
|
|
if (typeof frame === 'string')
|
2013-09-09 09:30:01 +00:00
|
|
|
{
|
2014-10-29 06:26:35 +00:00
|
|
|
this.frameName = frame;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (typeof frame === 'number')
|
|
|
|
{
|
|
|
|
this.frame = frame;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-30 16:12:22 +00:00
|
|
|
|
2014-10-29 06:26:35 +00:00
|
|
|
};
|
2013-09-30 16:12:22 +00:00
|
|
|
|
2014-10-29 06:26:35 +00:00
|
|
|
/**
|
|
|
|
* Used to manually set the frames that will be used for the different states of the Button.
|
|
|
|
*
|
|
|
|
* Frames can be specified as either an integer (the frame ID) or a string (the frame name); these are the same values that can be used with a Sprite constructor.
|
|
|
|
*
|
|
|
|
* @method Phaser.Button#setFrames
|
|
|
|
* @public
|
|
|
|
* @param {string|integer} [overFrame] - The frame / frameName when the button is in the Over state.
|
|
|
|
* @param {string|integer} [outFrame] - The frame / frameName when the button is in the Out state.
|
|
|
|
* @param {string|integer} [downFrame] - The frame / frameName when the button is in the Down state.
|
|
|
|
* @param {string|integer} [upFrame] - The frame / frameName when the button is in the Up state.
|
|
|
|
*/
|
|
|
|
Phaser.Button.prototype.setFrames = function (overFrame, outFrame, downFrame, upFrame) {
|
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
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());
|
2014-10-29 06:26:35 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the sound/marker for the given state.
|
|
|
|
*
|
|
|
|
* @method Phaser.Button#setStateSound
|
|
|
|
* @private
|
2014-10-30 02:31:03 +00:00
|
|
|
* @param {object} state - See `STATE_*`
|
2014-10-29 06:26:35 +00:00
|
|
|
* @param {Phaser.Sound|Phaser.AudioSprite} [sound] - Sound.
|
2014-10-30 02:31:03 +00:00
|
|
|
* @param {string} [marker=''] - Sound marker.
|
2014-10-29 06:26:35 +00:00
|
|
|
*/
|
|
|
|
Phaser.Button.prototype.setStateSound = function (state, sound, marker) {
|
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
var soundKey = 'on' + state + 'Sound';
|
|
|
|
var markerKey = 'on' + state + 'SoundMarker';
|
2014-10-29 06:26:35 +00:00
|
|
|
|
|
|
|
if (sound instanceof Phaser.Sound || sound instanceof Phaser.AudioSprite)
|
|
|
|
{
|
2014-10-30 02:31:03 +00:00
|
|
|
this[soundKey] = sound;
|
|
|
|
this[markerKey] = typeof marker === 'string' ? marker : '';
|
2014-10-29 06:26:35 +00:00
|
|
|
}
|
2014-10-30 02:31:03 +00:00
|
|
|
else
|
2013-12-31 17:03:09 +00:00
|
|
|
{
|
2014-10-30 02:31:03 +00:00
|
|
|
this[soundKey] = null;
|
|
|
|
this[markerKey] = '';
|
2014-10-29 06:26:35 +00:00
|
|
|
}
|
2013-12-31 17:03:09 +00:00
|
|
|
|
2014-10-29 06:26:35 +00:00
|
|
|
};
|
2013-12-31 17:03:09 +00:00
|
|
|
|
2014-10-29 06:26:35 +00:00
|
|
|
/**
|
|
|
|
* Play the sound for the given state, _if_ the state has a sound assigned.
|
|
|
|
*
|
|
|
|
* @method Phaser.Button#playStateSound
|
|
|
|
* @private
|
2014-10-30 02:31:03 +00:00
|
|
|
* @param {object} state - See `STATE_*`
|
2014-10-29 06:26:35 +00:00
|
|
|
* @return {boolean} True only if a sound was played.
|
|
|
|
*/
|
|
|
|
Phaser.Button.prototype.playStateSound = function (state) {
|
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
var soundKey = 'on' + state + 'Sound';
|
|
|
|
var sound = this[soundKey];
|
2014-10-29 06:26:35 +00:00
|
|
|
|
|
|
|
if (sound)
|
|
|
|
{
|
2014-10-30 02:31:03 +00:00
|
|
|
var markerKey = 'on' + state + 'SoundMarker';
|
|
|
|
var marker = this[markerKey];
|
|
|
|
|
2014-10-29 06:26:35 +00:00
|
|
|
sound.play(marker);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
2013-12-31 17:03:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-09 09:30:01 +00:00
|
|
|
};
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-30 03:46:52 +00:00
|
|
|
* Sets the sounds to be played whenever this Button is interacted with. Sounds can be either full Sound objects, or markers pointing to a section of a Sound object.
|
|
|
|
* The most common forms of sounds are 'hover' effects and 'click' effects, which is why the order of the parameters is overSound then downSound.
|
|
|
|
*
|
2014-10-29 06:26:35 +00:00
|
|
|
* Call this function with no parameters to reset all sounds on this Button.
|
|
|
|
*
|
|
|
|
* @method Phaser.Button#setSounds
|
|
|
|
* @public
|
2014-10-20 22:17:05 +00:00
|
|
|
* @param {Phaser.Sound|Phaser.AudioSprite} [overSound] - Over Button Sound.
|
2013-10-30 03:46:52 +00:00
|
|
|
* @param {string} [overMarker] - Over Button Sound Marker.
|
2014-10-20 22:17:05 +00:00
|
|
|
* @param {Phaser.Sound|Phaser.AudioSprite} [downSound] - Down Button Sound.
|
2013-10-30 03:46:52 +00:00
|
|
|
* @param {string} [downMarker] - Down Button Sound Marker.
|
2014-10-20 22:17:05 +00:00
|
|
|
* @param {Phaser.Sound|Phaser.AudioSprite} [outSound] - Out Button Sound.
|
2013-10-30 03:46:52 +00:00
|
|
|
* @param {string} [outMarker] - Out Button Sound Marker.
|
2014-10-20 22:17:05 +00:00
|
|
|
* @param {Phaser.Sound|Phaser.AudioSprite} [upSound] - Up Button Sound.
|
2013-10-30 03:46:52 +00:00
|
|
|
* @param {string} [upMarker] - Up Button Sound Marker.
|
|
|
|
*/
|
|
|
|
Phaser.Button.prototype.setSounds = function (overSound, overMarker, downSound, downMarker, outSound, outMarker, upSound, upMarker) {
|
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
this.setStateSound(STATE_OVER, overSound, overMarker);
|
|
|
|
this.setStateSound(STATE_OUT, outSound, outMarker);
|
|
|
|
this.setStateSound(STATE_DOWN, downSound, downMarker);
|
|
|
|
this.setStateSound(STATE_UP, upSound, upMarker);
|
2013-10-30 03:46:52 +00:00
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2013-10-30 03:46:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Sound to be played when a Pointer moves over this Button.
|
|
|
|
*
|
2014-10-29 06:26:35 +00:00
|
|
|
* @method Phaser.Button#setOverSound
|
|
|
|
* @public
|
2014-10-20 22:17:05 +00:00
|
|
|
* @param {Phaser.Sound|Phaser.AudioSprite} sound - The Sound that will be played.
|
2013-10-30 03:46:52 +00:00
|
|
|
* @param {string} [marker] - A Sound Marker that will be used in the playback.
|
|
|
|
*/
|
|
|
|
Phaser.Button.prototype.setOverSound = function (sound, marker) {
|
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
this.setStateSound(STATE_OVER, sound, marker);
|
2013-10-30 03:46:52 +00:00
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2013-10-30 03:46:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Sound to be played when a Pointer moves out of this Button.
|
|
|
|
*
|
2014-10-29 06:26:35 +00:00
|
|
|
* @method Phaser.Button#setOutSound
|
|
|
|
* @public
|
2014-10-20 22:17:05 +00:00
|
|
|
* @param {Phaser.Sound|Phaser.AudioSprite} sound - The Sound that will be played.
|
2013-10-30 03:46:52 +00:00
|
|
|
* @param {string} [marker] - A Sound Marker that will be used in the playback.
|
|
|
|
*/
|
|
|
|
Phaser.Button.prototype.setOutSound = function (sound, marker) {
|
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
this.setStateSound(STATE_OUT, sound, marker);
|
2013-10-30 03:46:52 +00:00
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2013-10-30 03:46:52 +00:00
|
|
|
|
|
|
|
/**
|
2013-12-31 17:03:09 +00:00
|
|
|
* The Sound to be played when a Pointer presses down on this Button.
|
2013-10-01 12:54:29 +00:00
|
|
|
*
|
2014-10-29 06:26:35 +00:00
|
|
|
* @method Phaser.Button#setDownSound
|
|
|
|
* @public
|
2014-10-20 22:17:05 +00:00
|
|
|
* @param {Phaser.Sound|Phaser.AudioSprite} sound - The Sound that will be played.
|
2013-10-30 03:46:52 +00:00
|
|
|
* @param {string} [marker] - A Sound Marker that will be used in the playback.
|
|
|
|
*/
|
2013-12-31 17:03:09 +00:00
|
|
|
Phaser.Button.prototype.setDownSound = function (sound, marker) {
|
2013-10-30 03:46:52 +00:00
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
this.setStateSound(STATE_DOWN, sound, marker);
|
2013-10-30 03:46:52 +00:00
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2013-10-30 03:46:52 +00:00
|
|
|
|
|
|
|
/**
|
2013-12-31 17:03:09 +00:00
|
|
|
* The Sound to be played when a Pointer has pressed down and is released from this Button.
|
2013-10-30 03:46:52 +00:00
|
|
|
*
|
2014-10-29 06:26:35 +00:00
|
|
|
* @method Phaser.Button#setUpSound
|
|
|
|
* @public
|
2014-10-20 22:17:05 +00:00
|
|
|
* @param {Phaser.Sound|Phaser.AudioSprite} sound - The Sound that will be played.
|
2013-10-30 03:46:52 +00:00
|
|
|
* @param {string} [marker] - A Sound Marker that will be used in the playback.
|
|
|
|
*/
|
2013-12-31 17:03:09 +00:00
|
|
|
Phaser.Button.prototype.setUpSound = function (sound, marker) {
|
2013-10-30 03:46:52 +00:00
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
this.setStateSound(STATE_UP, sound, marker);
|
2013-10-30 03:46:52 +00:00
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2013-10-30 03:46:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal function that handles input events.
|
|
|
|
*
|
2014-10-29 06:26:35 +00:00
|
|
|
* @method Phaser.Button#onInputOverHandler
|
2013-10-30 03:46:52 +00:00
|
|
|
* @protected
|
2015-01-28 17:18:19 +00:00
|
|
|
* @param {Phaser.Button} sprite - The Button that the event occurred on.
|
2013-10-30 03:46:52 +00:00
|
|
|
* @param {Phaser.Pointer} pointer - The Pointer that activated the Button.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-12-31 17:03:09 +00:00
|
|
|
Phaser.Button.prototype.onInputOverHandler = function (sprite, pointer) {
|
2013-09-08 23:30:44 +00:00
|
|
|
|
2014-10-21 21:43:42 +00:00
|
|
|
// If the Pointer was only just released then we don't fire an over event
|
|
|
|
if (pointer.justReleased())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
this.changeStateFrame(STATE_OVER);
|
2013-09-08 23:30:44 +00:00
|
|
|
|
2014-05-13 10:04:39 +00:00
|
|
|
if (this.onOverMouseOnly && !pointer.isMouse)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
this.playStateSound(STATE_OVER);
|
2013-10-30 03:46:52 +00:00
|
|
|
|
2013-09-08 23:30:44 +00:00
|
|
|
if (this.onInputOver)
|
|
|
|
{
|
|
|
|
this.onInputOver.dispatch(this, pointer);
|
|
|
|
}
|
2014-05-13 10:04:39 +00:00
|
|
|
|
2013-09-08 23:30:44 +00:00
|
|
|
};
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-30 03:46:52 +00:00
|
|
|
* Internal function that handles input events.
|
2013-10-01 12:54:29 +00:00
|
|
|
*
|
2014-10-29 06:26:35 +00:00
|
|
|
* @method Phaser.Button#onInputOutHandler
|
2013-10-30 03:46:52 +00:00
|
|
|
* @protected
|
2015-01-28 17:18:19 +00:00
|
|
|
* @param {Phaser.Button} sprite - The Button that the event occurred on.
|
2013-10-30 03:46:52 +00:00
|
|
|
* @param {Phaser.Pointer} pointer - The Pointer that activated the Button.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-12-31 17:03:09 +00:00
|
|
|
Phaser.Button.prototype.onInputOutHandler = function (sprite, pointer) {
|
2013-09-08 23:30:44 +00:00
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
this.changeStateFrame(STATE_OUT);
|
2013-09-08 23:30:44 +00:00
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
this.playStateSound(STATE_OUT);
|
2013-10-30 03:46:52 +00:00
|
|
|
|
2013-09-08 23:30:44 +00:00
|
|
|
if (this.onInputOut)
|
|
|
|
{
|
|
|
|
this.onInputOut.dispatch(this, pointer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-30 03:46:52 +00:00
|
|
|
* Internal function that handles input events.
|
2013-10-01 12:54:29 +00:00
|
|
|
*
|
2014-10-29 06:26:35 +00:00
|
|
|
* @method Phaser.Button#onInputDownHandler
|
2013-10-30 03:46:52 +00:00
|
|
|
* @protected
|
2015-01-28 17:18:19 +00:00
|
|
|
* @param {Phaser.Button} sprite - The Button that the event occurred on.
|
2013-10-30 03:46:52 +00:00
|
|
|
* @param {Phaser.Pointer} pointer - The Pointer that activated the Button.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-12-31 17:03:09 +00:00
|
|
|
Phaser.Button.prototype.onInputDownHandler = function (sprite, pointer) {
|
2013-09-08 23:30:44 +00:00
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
this.changeStateFrame(STATE_DOWN);
|
2013-09-08 23:30:44 +00:00
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
this.playStateSound(STATE_DOWN);
|
2013-10-30 03:46:52 +00:00
|
|
|
|
2013-09-08 23:30:44 +00:00
|
|
|
if (this.onInputDown)
|
|
|
|
{
|
|
|
|
this.onInputDown.dispatch(this, pointer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-30 03:46:52 +00:00
|
|
|
* Internal function that handles input events.
|
2013-10-01 12:54:29 +00:00
|
|
|
*
|
2014-10-29 06:26:35 +00:00
|
|
|
* @method Phaser.Button#onInputUpHandler
|
2013-10-30 03:46:52 +00:00
|
|
|
* @protected
|
2015-01-28 17:18:19 +00:00
|
|
|
* @param {Phaser.Button} sprite - The Button that the event occurred on.
|
2013-10-30 03:46:52 +00:00
|
|
|
* @param {Phaser.Pointer} pointer - The Pointer that activated the Button.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-12-31 17:03:09 +00:00
|
|
|
Phaser.Button.prototype.onInputUpHandler = function (sprite, pointer, isOver) {
|
2013-09-08 23:30:44 +00:00
|
|
|
|
2014-10-30 02:31:03 +00:00
|
|
|
this.playStateSound(STATE_UP);
|
2013-12-31 17:03:09 +00:00
|
|
|
|
2014-10-29 06:26:35 +00:00
|
|
|
// Input dispatched early, before state change (but after sound)
|
2013-12-31 17:03:09 +00:00
|
|
|
if (this.onInputUp)
|
|
|
|
{
|
|
|
|
this.onInputUp.dispatch(this, pointer, isOver);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.freezeFrames)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.forceOut)
|
|
|
|
{
|
2014-10-30 02:31:03 +00:00
|
|
|
this.changeStateFrame(STATE_OUT);
|
2013-12-31 17:03:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-30 02:31:03 +00:00
|
|
|
var changedUp = this.changeStateFrame(STATE_UP);
|
2014-10-29 06:26:35 +00:00
|
|
|
if (!changedUp)
|
2013-10-02 19:18:24 +00:00
|
|
|
{
|
2014-10-29 06:26:35 +00:00
|
|
|
// No Up frame to show..
|
2013-12-31 17:03:09 +00:00
|
|
|
if (isOver)
|
|
|
|
{
|
2014-10-30 02:31:03 +00:00
|
|
|
this.changeStateFrame(STATE_OVER);
|
2013-12-31 17:03:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-30 02:31:03 +00:00
|
|
|
this.changeStateFrame(STATE_OUT);
|
2013-12-31 17:03:09 +00:00
|
|
|
}
|
2013-10-02 19:18:24 +00:00
|
|
|
}
|
2013-09-08 23:30:44 +00:00
|
|
|
}
|
|
|
|
|
2013-12-31 17:03:09 +00:00
|
|
|
};
|