forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDestroy.js
More file actions
165 lines (134 loc) · 4.14 KB
/
Copy pathDestroy.js
File metadata and controls
165 lines (134 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 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.
*
* @class
*/
Phaser.Component.Destroy = function () {};
Phaser.Component.Destroy.prototype = {
/**
* 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
* @readOnly
*/
destroyPhase: false,
/**
* 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.
*
* You can optionally also destroy the BaseTexture this Game Object is using. Be careful if you've
* more than one Game Object sharing the same BaseTexture.
*
* @method
* @param {boolean} [destroyChildren=true] - Should every child of this object have its destroy method called as well?
* @param {boolean} [destroyTexture=false] - Destroy the BaseTexture this Game Object is using? Note that if another Game Object is sharing the same BaseTexture it will invalidate it.
*/
destroy: function (destroyChildren, destroyTexture) {
if (this.game === null || this.destroyPhase) { return; }
if (destroyChildren === undefined) { destroyChildren = true; }
if (destroyTexture === undefined) { destroyTexture = false; }
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();
}
this.game.tweens.removeFrom(this);
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;
this.cropRect = null;
}
if (this._frame)
{
this._frame = null;
}
if (Phaser.Video && this.key instanceof Phaser.Video)
{
this.key.onChangeSource.remove(this.resizeFrame, this);
}
if (Phaser.BitmapText && this._glyphs)
{
this._glyphs = [];
}
this.alive = false;
this.exists = false;
this.visible = false;
this.filters = null;
this.mask = null;
this.game = null;
this.data = {};
// In case Pixi is still going to try and render it even though destroyed
this.renderable = false;
if (this.transformCallback)
{
this.transformCallback = null;
this.transformCallbackContext = null;
}
// Pixi level DisplayObject destroy
this.hitArea = null;
this.parent = null;
this.stage = null;
this.worldTransform = null;
this.filterArea = null;
this._bounds = null;
this._currentBounds = null;
this._mask = null;
this._destroyCachedSprite();
// Texture?
if (destroyTexture)
{
this.texture.destroy(true);
}
this.destroyPhase = false;
this.pendingDestroy = false;
}
};