2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2016-04-04 21:15:01 +00:00
|
|
|
* @copyright 2016 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-08 21:38:19 +00:00
|
|
|
/**
|
2014-09-16 16:35:08 +00:00
|
|
|
* The Events component is a collection of events fired by the parent game object.
|
2016-05-04 01:02:13 +00:00
|
|
|
*
|
|
|
|
* Phaser uses what are known as 'Signals' for all event handling. All of the events in
|
|
|
|
* this class are signals you can subscribe to, much in the same way you'd "listen" for
|
|
|
|
* an event.
|
2013-11-28 15:57:09 +00:00
|
|
|
*
|
2016-05-04 01:02:13 +00:00
|
|
|
* For example to tell when a Sprite has been added to a new group, you can bind a function
|
|
|
|
* to the `onAddedToGroup` signal:
|
2013-11-28 15:57:09 +00:00
|
|
|
*
|
2014-03-03 16:05:55 +00:00
|
|
|
* `sprite.events.onAddedToGroup.add(yourFunction, this);`
|
2013-11-28 15:57:09 +00:00
|
|
|
*
|
|
|
|
* Where `yourFunction` is the function you want called when this event occurs.
|
2016-05-04 01:02:13 +00:00
|
|
|
*
|
|
|
|
* For more details about how signals work please see the Phaser.Signal class.
|
2013-11-28 15:57:09 +00:00
|
|
|
*
|
2014-12-01 03:49:15 +00:00
|
|
|
* The Input-related events will only be dispatched if the Sprite has had `inputEnabled` set to `true`
|
|
|
|
* and the Animation-related events only apply to game objects with animations like {@link Phaser.Sprite}.
|
2013-11-28 15:57:09 +00:00
|
|
|
*
|
2014-09-16 16:35:08 +00:00
|
|
|
* @class Phaser.Events
|
2013-10-01 12:54:29 +00:00
|
|
|
* @constructor
|
2014-12-01 03:49:15 +00:00
|
|
|
* @param {Phaser.Sprite} sprite - A reference to the game object / Sprite that owns this Events object.
|
2013-09-08 21:38:19 +00:00
|
|
|
*/
|
|
|
|
Phaser.Events = function (sprite) {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-08-28 01:56:06 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Sprite} parent - The Sprite that owns these events.
|
|
|
|
*/
|
2013-11-25 03:13:04 +00:00
|
|
|
this.parent = sprite;
|
2014-02-05 02:39:03 +00:00
|
|
|
|
2014-12-01 03:49:15 +00:00
|
|
|
// The signals are automatically added by the corresponding proxy properties
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Events.prototype = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all events.
|
|
|
|
*
|
|
|
|
* @method Phaser.Events#destroy
|
|
|
|
*/
|
|
|
|
destroy: function () {
|
|
|
|
|
|
|
|
this._parent = null;
|
|
|
|
|
|
|
|
if (this._onDestroy) { this._onDestroy.dispose(); }
|
|
|
|
if (this._onAddedToGroup) { this._onAddedToGroup.dispose(); }
|
|
|
|
if (this._onRemovedFromGroup) { this._onRemovedFromGroup.dispose(); }
|
|
|
|
if (this._onRemovedFromWorld) { this._onRemovedFromWorld.dispose(); }
|
|
|
|
if (this._onKilled) { this._onKilled.dispose(); }
|
|
|
|
if (this._onRevived) { this._onRevived.dispose(); }
|
2015-01-02 21:53:20 +00:00
|
|
|
if (this._onEnterBounds) { this._onEnterBounds.dispose(); }
|
2014-12-01 03:49:15 +00:00
|
|
|
if (this._onOutOfBounds) { this._onOutOfBounds.dispose(); }
|
|
|
|
|
|
|
|
if (this._onInputOver) { this._onInputOver.dispose(); }
|
|
|
|
if (this._onInputOut) { this._onInputOut.dispose(); }
|
|
|
|
if (this._onInputDown) { this._onInputDown.dispose(); }
|
|
|
|
if (this._onInputUp) { this._onInputUp.dispose(); }
|
|
|
|
if (this._onDragStart) { this._onDragStart.dispose(); }
|
2015-06-04 19:58:00 +00:00
|
|
|
if (this._onDragUpdate) { this._onDragUpdate.dispose(); }
|
2014-12-01 03:49:15 +00:00
|
|
|
if (this._onDragStop) { this._onDragStop.dispose(); }
|
|
|
|
|
|
|
|
if (this._onAnimationStart) { this._onAnimationStart.dispose(); }
|
|
|
|
if (this._onAnimationComplete) { this._onAnimationComplete.dispose(); }
|
|
|
|
if (this._onAnimationLoop) { this._onAnimationLoop.dispose(); }
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
// The following properties are sentinels that will be replaced with getters
|
|
|
|
|
2014-08-28 01:56:06 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onAddedToGroup - This signal is dispatched when the parent is added to a new Group.
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onAddedToGroup: null,
|
2014-08-28 01:56:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onRemovedFromGroup - This signal is dispatched when the parent is removed from a Group.
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onRemovedFromGroup: null,
|
2014-08-28 01:56:06 +00:00
|
|
|
|
2014-10-28 01:48:42 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onRemovedFromWorld - This signal is dispatched if this item or any of its parents are removed from the game world.
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onRemovedFromWorld: null,
|
2014-10-28 01:48:42 +00:00
|
|
|
|
2014-08-28 01:56:06 +00:00
|
|
|
/**
|
2015-03-18 19:14:31 +00:00
|
|
|
* @property {Phaser.Signal} onDestroy - This signal is dispatched when the parent is destroyed.
|
2014-08-28 01:56:06 +00:00
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onDestroy: null,
|
2014-08-28 01:56:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onKilled - This signal is dispatched when the parent is killed.
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onKilled: null,
|
2014-08-28 01:56:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onRevived - This signal is dispatched when the parent is revived.
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onRevived: null,
|
2014-08-28 01:56:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onOutOfBounds - This signal is dispatched when the parent leaves the world bounds (only if Sprite.checkWorldBounds is true).
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onOutOfBounds: null,
|
2014-08-28 01:56:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onEnterBounds - This signal is dispatched when the parent returns within the world bounds (only if Sprite.checkWorldBounds is true).
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onEnterBounds: null,
|
2013-09-08 21:38:19 +00:00
|
|
|
|
2014-08-28 01:56:06 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onInputOver - This signal is dispatched if the parent is inputEnabled and receives an over event from a Pointer.
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onInputOver: null,
|
2014-08-28 01:56:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onInputOut - This signal is dispatched if the parent is inputEnabled and receives an out event from a Pointer.
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onInputOut: null,
|
2014-08-28 01:56:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onInputDown - This signal is dispatched if the parent is inputEnabled and receives a down event from a Pointer.
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onInputDown: null,
|
2014-08-28 01:56:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onInputUp - This signal is dispatched if the parent is inputEnabled and receives an up event from a Pointer.
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onInputUp: null,
|
2014-08-28 01:56:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onDragStart - This signal is dispatched if the parent is inputEnabled and receives a drag start event from a Pointer.
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onDragStart: null,
|
2014-08-28 01:56:06 +00:00
|
|
|
|
2015-06-04 19:58:00 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onDragUpdate - This signal is dispatched if the parent is inputEnabled and receives a drag update event from a Pointer.
|
|
|
|
*/
|
|
|
|
onDragUpdate: null,
|
|
|
|
|
2014-08-28 01:56:06 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onDragStop - This signal is dispatched if the parent is inputEnabled and receives a drag stop event from a Pointer.
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onDragStop: null,
|
2013-09-08 21:38:19 +00:00
|
|
|
|
2014-08-28 01:56:06 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onAnimationStart - This signal is dispatched when the parent has an animation that is played.
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onAnimationStart: null,
|
2014-08-28 01:56:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onAnimationComplete - This signal is dispatched when the parent has an animation that finishes playing.
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onAnimationComplete: null,
|
2014-08-28 01:56:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onAnimationLoop - This signal is dispatched when the parent has an animation that loops playback.
|
|
|
|
*/
|
2014-12-01 03:49:15 +00:00
|
|
|
onAnimationLoop: null
|
2013-09-10 19:40:34 +00:00
|
|
|
|
2013-10-13 00:29:57 +00:00
|
|
|
};
|
|
|
|
|
2014-12-01 03:49:15 +00:00
|
|
|
Phaser.Events.prototype.constructor = Phaser.Events;
|
2013-10-13 00:29:57 +00:00
|
|
|
|
2014-12-01 03:49:15 +00:00
|
|
|
// Create an auto-create proxy getter and dispatch method for all events.
|
|
|
|
// The backing property is the same as the event name, prefixed with '_'
|
|
|
|
// and the dispatch method is the same as the event name postfixed with '$dispatch'.
|
|
|
|
for (var prop in Phaser.Events.prototype)
|
|
|
|
{
|
|
|
|
if (!Phaser.Events.prototype.hasOwnProperty(prop) ||
|
|
|
|
prop.indexOf('on') !== 0 ||
|
|
|
|
Phaser.Events.prototype[prop] !== null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2013-10-13 00:29:57 +00:00
|
|
|
|
2015-01-01 21:50:07 +00:00
|
|
|
(function (prop, backing) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// The accessor creates a new Signal; and so it should only be used from user-code.
|
|
|
|
Object.defineProperty(Phaser.Events.prototype, prop, {
|
|
|
|
get: function () {
|
|
|
|
return this[backing] || (this[backing] = new Phaser.Signal());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// The dispatcher will only broadcast on an already-created signal; call this internally.
|
|
|
|
Phaser.Events.prototype[prop + '$dispatch'] = function () {
|
|
|
|
return this[backing] ? this[backing].dispatch.apply(this[backing], arguments) : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
})(prop, '_' + prop);
|
2014-12-01 03:49:15 +00:00
|
|
|
|
|
|
|
}
|