2015-03-01 07:00:17 +00:00
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2015 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Destroy component is responsible for destroying a Game Object.
|
2015-03-01 07:00:17 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*/
|
2015-02-17 05:15:04 +00:00
|
|
|
Phaser.Component.Destroy = function () {};
|
|
|
|
|
|
|
|
Phaser.Component.Destroy.prototype = {
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* As a Game Object runs through its destroy method this flag is set to true,
|
|
|
|
* and can be checked in any sub-systems or plugins it is being destroyed from.
|
|
|
|
* @property {boolean} destroyPhase
|
2015-02-17 05:15:04 +00:00
|
|
|
* @readOnly
|
|
|
|
*/
|
|
|
|
destroyPhase: false,
|
|
|
|
|
|
|
|
/**
|
2015-03-23 23:27:14 +00:00
|
|
|
* Destroys the Game Object. This removes it from its parent group, destroys the input, event and animation handlers if present
|
|
|
|
* and nulls its reference to `game`, freeing it up for garbage collection.
|
|
|
|
*
|
|
|
|
* If this Game Object has the Events component it will also dispatch the `onDestroy` event.
|
2015-02-17 05:15:04 +00:00
|
|
|
*
|
2015-03-01 07:00:17 +00:00
|
|
|
* @method
|
2015-03-23 23:27:14 +00:00
|
|
|
* @param {boolean} [destroyChildren=true] - Should every child of this object have its destroy method called as well?
|
2015-02-17 05:15:04 +00:00
|
|
|
*/
|
2015-07-08 17:52:01 +00:00
|
|
|
destroy: function (destroyChildren) {
|
2015-02-17 05:15:04 +00:00
|
|
|
|
|
|
|
if (this.game === null || this.destroyPhase) { return; }
|
|
|
|
|
2015-07-22 09:37:15 +00:00
|
|
|
if (destroyChildren === undefined) { destroyChildren = true; }
|
2015-02-17 05:15:04 +00:00
|
|
|
|
|
|
|
this.destroyPhase = true;
|
|
|
|
|
|
|
|
if (this.events)
|
|
|
|
{
|
|
|
|
this.events.onDestroy$dispatch(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.parent)
|
|
|
|
{
|
|
|
|
if (this.parent instanceof Phaser.Group)
|
|
|
|
{
|
|
|
|
this.parent.remove(this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.parent.removeChild(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.input)
|
|
|
|
{
|
|
|
|
this.input.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.animations)
|
|
|
|
{
|
|
|
|
this.animations.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.body)
|
|
|
|
{
|
|
|
|
this.body.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.events)
|
|
|
|
{
|
|
|
|
this.events.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
var i = this.children.length;
|
|
|
|
|
|
|
|
if (destroyChildren)
|
|
|
|
{
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
this.children[i].destroy(destroyChildren);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
this.removeChild(this.children[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._crop)
|
|
|
|
{
|
|
|
|
this._crop = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._frame)
|
|
|
|
{
|
|
|
|
this._frame = null;
|
|
|
|
}
|
|
|
|
|
2015-05-05 23:42:01 +00:00
|
|
|
if (Phaser.Video && this.key instanceof Phaser.Video)
|
|
|
|
{
|
|
|
|
this.key.onChangeSource.remove(this.resizeFrame, this);
|
|
|
|
}
|
|
|
|
|
2015-06-03 04:27:57 +00:00
|
|
|
if (Phaser.BitmapText && this._glyphs)
|
|
|
|
{
|
|
|
|
this._glyphs = [];
|
|
|
|
}
|
|
|
|
|
2015-02-17 05:15:04 +00:00
|
|
|
this.alive = false;
|
|
|
|
this.exists = false;
|
|
|
|
this.visible = false;
|
|
|
|
|
|
|
|
this.filters = null;
|
|
|
|
this.mask = null;
|
|
|
|
this.game = null;
|
|
|
|
|
2015-03-11 01:44:46 +00:00
|
|
|
// In case Pixi is still going to try and render it even though destroyed
|
|
|
|
this.renderable = false;
|
|
|
|
|
|
|
|
// Pixi level DisplayObject destroy
|
|
|
|
this.transformCallback = null;
|
|
|
|
this.transformCallbackContext = null;
|
|
|
|
this.hitArea = null;
|
|
|
|
this.parent = null;
|
|
|
|
this.stage = null;
|
|
|
|
this.worldTransform = null;
|
|
|
|
this.filterArea = null;
|
|
|
|
this._bounds = null;
|
|
|
|
this._currentBounds = null;
|
|
|
|
this._mask = null;
|
|
|
|
|
2015-08-03 13:46:55 +00:00
|
|
|
|
|
|
|
|
2015-03-11 01:44:46 +00:00
|
|
|
this._destroyCachedSprite();
|
|
|
|
|
2015-02-17 05:15:04 +00:00
|
|
|
this.destroyPhase = false;
|
2015-07-08 17:52:01 +00:00
|
|
|
this.pendingDestroy = false;
|
2015-02-17 05:15:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|