Skip to content

Commit fa45d7f

Browse files
committed
Events.onDestroy is a new signal that is dispatched whenever the parent is being destroyed. It's dispatched at the start of the destroy process, allowing you to perform any additional house cleaning needed (thanks @jonkelling phaserjs#1084)
Group.onDestroy is a new signal that is dispatched whenever the Group is being destroyed. It's dispatched at the start of the destroy process, allowing you to perform any additional house cleaning needed (thanks @jonkelling phaserjs#1084)
1 parent 6b4510f commit fa45d7f

8 files changed

Lines changed: 115 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ Version 2.1.0 - "Cairhien" - -in development-
8383
* Phaser.Device.isAndroidStockBrowser will inform you if your game is running in a stock Android browser (rather than Chrome) where you may wish to scale down effects, disable WebGL, etc (thanks @lucbloom #989)
8484
* Phaser.Camera has a new property `position` which is a Point object that allows you to get or set the camera position without having to read both the x and y values (thanks @Zielak #1015)
8585
* TileSprite now has the `alive` property, which should help with some Group operations (thanks @jonkelling #1085)
86+
* Events.onDestroy is a new signal that is dispatched whenever the parent is being destroyed. It's dispatched at the start of the destroy process, allowing you to perform any additional house cleaning needed (thanks @jonkelling #1084)
87+
* Group.onDestroy is a new signal that is dispatched whenever the Group is being destroyed. It's dispatched at the start of the destroy process, allowing you to perform any additional house cleaning needed (thanks @jonkelling #1084)
8688

8789
### Updates
8890

src/core/Group.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ Phaser.Group = function (game, parent, name, addToStage, enableBody, physicsBody
123123
*/
124124
this.physicsBodyType = physicsBodyType;
125125

126+
/**
127+
* @property {Phaser.Signal} onDestroy - This signal is dispatched when the parent is destoyed.
128+
*/
129+
this.onDestroy = new Phaser.Signal();
130+
126131
/**
127132
* @property {string} _sortProperty - The property on which children are sorted.
128133
* @private
@@ -1688,6 +1693,8 @@ Phaser.Group.prototype.destroy = function (destroyChildren, soft) {
16881693
if (typeof destroyChildren === 'undefined') { destroyChildren = true; }
16891694
if (typeof soft === 'undefined') { soft = false; }
16901695

1696+
this.onDestroy.dispatch(this, destroyChildren, soft);
1697+
16911698
this.removeAll(destroyChildren);
16921699

16931700
this.cursor = null;

src/gameobjects/Events.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,114 @@
2323
*/
2424
Phaser.Events = function (sprite) {
2525

26+
/**
27+
* @property {Phaser.Sprite} parent - The Sprite that owns these events.
28+
*/
2629
this.parent = sprite;
2730

31+
/**
32+
* @property {Phaser.Signal} onAddedToGroup - This signal is dispatched when the parent is added to a new Group.
33+
*/
2834
this.onAddedToGroup = new Phaser.Signal();
35+
36+
/**
37+
* @property {Phaser.Signal} onRemovedFromGroup - This signal is dispatched when the parent is removed from a Group.
38+
*/
2939
this.onRemovedFromGroup = new Phaser.Signal();
40+
41+
/**
42+
* @property {Phaser.Signal} onDestroy - This signal is dispatched when the parent is destoyed.
43+
*/
44+
this.onDestroy = new Phaser.Signal();
45+
46+
/**
47+
* @property {Phaser.Signal} onKilled - This signal is dispatched when the parent is killed.
48+
*/
3049
this.onKilled = new Phaser.Signal();
50+
51+
/**
52+
* @property {Phaser.Signal} onRevived - This signal is dispatched when the parent is revived.
53+
*/
3154
this.onRevived = new Phaser.Signal();
55+
56+
/**
57+
* @property {Phaser.Signal} onOutOfBounds - This signal is dispatched when the parent leaves the world bounds (only if Sprite.checkWorldBounds is true).
58+
*/
3259
this.onOutOfBounds = new Phaser.Signal();
60+
61+
/**
62+
* @property {Phaser.Signal} onEnterBounds - This signal is dispatched when the parent returns within the world bounds (only if Sprite.checkWorldBounds is true).
63+
*/
3364
this.onEnterBounds = new Phaser.Signal();
3465

66+
/**
67+
* @property {Phaser.Signal} onInputOver - This signal is dispatched if the parent is inputEnabled and receives an over event from a Pointer.
68+
* @default null
69+
*/
3570
this.onInputOver = null;
71+
72+
/**
73+
* @property {Phaser.Signal} onInputOut - This signal is dispatched if the parent is inputEnabled and receives an out event from a Pointer.
74+
* @default null
75+
*/
3676
this.onInputOut = null;
77+
78+
/**
79+
* @property {Phaser.Signal} onInputDown - This signal is dispatched if the parent is inputEnabled and receives a down event from a Pointer.
80+
* @default null
81+
*/
3782
this.onInputDown = null;
83+
84+
/**
85+
* @property {Phaser.Signal} onInputUp - This signal is dispatched if the parent is inputEnabled and receives an up event from a Pointer.
86+
* @default null
87+
*/
3888
this.onInputUp = null;
89+
90+
/**
91+
* @property {Phaser.Signal} onDragStart - This signal is dispatched if the parent is inputEnabled and receives a drag start event from a Pointer.
92+
* @default null
93+
*/
3994
this.onDragStart = null;
95+
96+
/**
97+
* @property {Phaser.Signal} onDragStop - This signal is dispatched if the parent is inputEnabled and receives a drag stop event from a Pointer.
98+
* @default null
99+
*/
40100
this.onDragStop = null;
41101

102+
/**
103+
* @property {Phaser.Signal} onAnimationStart - This signal is dispatched when the parent has an animation that is played.
104+
* @default null
105+
*/
42106
this.onAnimationStart = null;
107+
108+
/**
109+
* @property {Phaser.Signal} onAnimationComplete - This signal is dispatched when the parent has an animation that finishes playing.
110+
* @default null
111+
*/
43112
this.onAnimationComplete = null;
113+
114+
/**
115+
* @property {Phaser.Signal} onAnimationLoop - This signal is dispatched when the parent has an animation that loops playback.
116+
* @default null
117+
*/
44118
this.onAnimationLoop = null;
45119

46120
};
47121

48122
Phaser.Events.prototype = {
49123

124+
/**
125+
* Removes all events.
126+
*
127+
* @method destroy
128+
*/
50129
destroy: function () {
51130

52131
this.parent = null;
132+
133+
this.onDestroy.dispose();
53134
this.onAddedToGroup.dispose();
54135
this.onRemovedFromGroup.dispose();
55136
this.onKilled.dispose();

src/gameobjects/Image.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,11 @@ Phaser.Image.prototype.destroy = function(destroyChildren) {
386386

387387
this._cache[8] = 1;
388388

389+
if (this.events)
390+
{
391+
this.events.onDestroy.dispatch(this);
392+
}
393+
389394
if (this.parent)
390395
{
391396
if (this.parent instanceof Phaser.Group)

src/gameobjects/Rope.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,11 @@ Phaser.Rope.prototype.destroy = function(destroyChildren) {
411411

412412
this._cache[8] = 1;
413413

414+
if (this.events)
415+
{
416+
this.events.onDestroy.dispatch(this);
417+
}
418+
414419
if (this.filters)
415420
{
416421
this.filters = null;

src/gameobjects/Sprite.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,11 @@ Phaser.Sprite.prototype.destroy = function(destroyChildren) {
644644

645645
this._cache[8] = 1;
646646

647+
if (this.events)
648+
{
649+
this.events.onDestroy.dispatch(this);
650+
}
651+
647652
if (this.parent)
648653
{
649654
if (this.parent instanceof Phaser.Group)

src/gameobjects/Text.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ Phaser.Text.prototype.destroy = function (destroyChildren) {
220220

221221
this._cache[8] = 1;
222222

223+
if (this.events)
224+
{
225+
this.events.onDestroy.dispatch(this);
226+
}
227+
223228
if (this.parent)
224229
{
225230
if (this.parent instanceof Phaser.Group)

src/gameobjects/TileSprite.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,11 @@ Phaser.TileSprite.prototype.destroy = function(destroyChildren) {
442442

443443
this._cache[8] = 1;
444444

445+
if (this.events)
446+
{
447+
this.events.onDestroy.dispatch(this);
448+
}
449+
445450
if (this.filters)
446451
{
447452
this.filters = null;

0 commit comments

Comments
 (0)