Skip to content

Commit

Permalink
All Game Objects have a new boolean property called pendingDestroy.…
Browse files Browse the repository at this point in the history
… If you set this to `true` then the object will automatically destroy itself in the *next* logic update, rather than immediately. This is useful for cases when you wish to destroy an object from within one of its own callbacks, such as with buttons or other input events (thanks @alamboley #1748)
  • Loading branch information
photonstorm committed Jul 8, 2015
1 parent 1b5da31 commit 3590272
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ Version 2.4 - "Katar" - in dev
* BitmapData.moveH(distance) allows you to horizontally shift the BitmapData with wrap-around the edges.
* BitmapData.moveV(distance) allows you to vertically shift the BitmapData with wrap-around the edges.
* Text.addStrokeColor works in the same way as `Text.addColor` but allows you to define a color stop for the stroke color instead of the fill color.
* All Game Objects have a new boolean property called `pendingDestroy`. If you set this to `true` then the object will automatically destroy itself in the *next* logic update, rather than immediately. This is useful for cases when you wish to destroy an object from within one of its own callbacks, such as with buttons or other input events (thanks @alamboley #1748)

### Updates

Expand Down
15 changes: 15 additions & 0 deletions src/gameobjects/components/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ Phaser.Component.Core.init = function (game, x, y, key, frame) {

Phaser.Component.Core.preUpdate = function () {

if (this.pendingDestroy)
{
this.destroy();
return;
}

this.previousPosition.set(this.world.x, this.world.y);
this.previousRotation = this.rotation;

Expand Down Expand Up @@ -232,6 +238,15 @@ Phaser.Component.Core.prototype = {
*/
fresh: true,

/**
* A Game Object is that is pendingDestroy is flagged to have its destroy method called on the next logic update.
* This is used internally by the likes of Buttons but you can also set it directly, to allow you to flag an
* object to be destroyed from within one of its own callbacks (such as a Button or Input event)
*
* @property {boolean} pendingDestroy
*/
pendingDestroy: false,

/**
* @property {Phaser.Rectangle} _bounds - Internal cache var.
* @private
Expand Down
3 changes: 2 additions & 1 deletion src/gameobjects/components/Destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Phaser.Component.Destroy.prototype = {
* @method
* @param {boolean} [destroyChildren=true] - Should every child of this object have its destroy method called as well?
*/
destroy: function(destroyChildren) {
destroy: function (destroyChildren) {

if (this.game === null || this.destroyPhase) { return; }

Expand Down Expand Up @@ -138,6 +138,7 @@ Phaser.Component.Destroy.prototype = {
this._destroyCachedSprite();

this.destroyPhase = false;
this.pendingDestroy = false;

}

Expand Down

0 comments on commit 3590272

Please sign in to comment.