Skip to content

Commit ee61d95

Browse files
committed
Tidying up the Pixi source.
Added DisplayObject.destroy to cleanly remove all references, callbacks and cached textures (fixes phaserjs#1656)
1 parent 0516167 commit ee61d95

2 files changed

Lines changed: 73 additions & 38 deletions

File tree

src/pixi/display/DisplayObject.js

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ PIXI.DisplayObject = function()
1717
* @property position
1818
* @type Point
1919
*/
20-
this.position = new PIXI.Point();
20+
this.position = new PIXI.Point(0, 0);
2121

2222
/**
2323
* The scale factor of the object.
2424
*
2525
* @property scale
2626
* @type Point
2727
*/
28-
this.scale = new PIXI.Point(1,1);//{x:1, y:1};
28+
this.scale = new PIXI.Point(1, 1);
2929

3030
/**
3131
* The transform callback is an optional callback that if set will be called at the end of the updateTransform method and sent two parameters:
@@ -52,7 +52,7 @@ PIXI.DisplayObject = function()
5252
* @property pivot
5353
* @type Point
5454
*/
55-
this.pivot = new PIXI.Point(0,0);
55+
this.pivot = new PIXI.Point(0, 0);
5656

5757
/**
5858
* The rotation of the object in radians.
@@ -87,14 +87,6 @@ PIXI.DisplayObject = function()
8787
*/
8888
this.hitArea = null;
8989

90-
/**
91-
* This is used to indicate if the displayObject should display a mouse hand cursor on rollover
92-
*
93-
* @property buttonMode
94-
* @type Boolean
95-
*/
96-
this.buttonMode = false;
97-
9890
/**
9991
* Can this object be rendered
10092
*
@@ -130,15 +122,6 @@ PIXI.DisplayObject = function()
130122
*/
131123
this.worldAlpha = 1;
132124

133-
/**
134-
* This is the cursor that will be used when the mouse is over this object. To enable this the element must have interaction = true and buttonMode = true
135-
*
136-
* @property defaultCursor
137-
* @type String
138-
*
139-
*/
140-
this.defaultCursor = 'pointer';
141-
142125
/**
143126
* [read-only] Current transform of the object based on world (parent) factors
144127
*
@@ -174,7 +157,7 @@ PIXI.DisplayObject = function()
174157
* @property filterArea
175158
* @type Rectangle
176159
*/
177-
this.filterArea = null;//new PIXI.Rectangle(0,0,1,1);
160+
this.filterArea = null;
178161

179162
/**
180163
* The original, cached bounds of the object
@@ -197,7 +180,7 @@ PIXI.DisplayObject = function()
197180
/**
198181
* The original, cached mask of the object
199182
*
200-
* @property _currentBounds
183+
* @property _mask
201184
* @type Rectangle
202185
* @private
203186
*/
@@ -226,25 +209,62 @@ PIXI.DisplayObject = function()
226209
// constructor
227210
PIXI.DisplayObject.prototype.constructor = PIXI.DisplayObject;
228211

212+
/**
213+
* Destroy this DisplayObject.
214+
* Removes all references to transformCallbacks, its parent, the stage, filters, bounds, mask and cached Sprites.
215+
*
216+
* @method destroy
217+
*/
218+
PIXI.DisplayObject.prototype.destroy = function()
219+
{
220+
if (this.children)
221+
{
222+
var i = this.children.length;
223+
224+
while (i--)
225+
{
226+
this.children[i].destroy();
227+
}
228+
229+
this.children = [];
230+
}
231+
232+
this.transformCallback = null;
233+
this.transformCallbackContext = null;
234+
this.hitArea = null;
235+
this.parent = null;
236+
this.stage = null;
237+
this.worldTransform = null;
238+
this.filterArea = null;
239+
this._bounds = null;
240+
this._currentBounds = null;
241+
this._mask = null;
242+
243+
this._destroyCachedSprite();
244+
};
245+
229246
/**
230247
* [read-only] Indicates if the sprite is globally visible.
231248
*
232249
* @property worldVisible
233250
* @type Boolean
234251
*/
235252
Object.defineProperty(PIXI.DisplayObject.prototype, 'worldVisible', {
253+
236254
get: function() {
255+
237256
var item = this;
238257

239258
do
240259
{
241-
if(!item.visible)return false;
260+
if (!item.visible) return false;
242261
item = item.parent;
243262
}
244263
while(item);
245264

246265
return true;
247266
}
267+
248268
});
249269

250270
/**
@@ -256,15 +276,20 @@ Object.defineProperty(PIXI.DisplayObject.prototype, 'worldVisible', {
256276
* @type Graphics
257277
*/
258278
Object.defineProperty(PIXI.DisplayObject.prototype, 'mask', {
279+
259280
get: function() {
260281
return this._mask;
261282
},
283+
262284
set: function(value) {
263285

264-
if(this._mask)this._mask.isMask = false;
286+
if (this._mask) this._mask.isMask = false;
287+
265288
this._mask = value;
266-
if(this._mask)this._mask.isMask = true;
289+
290+
if (this._mask) this._mask.isMask = true;
267291
}
292+
268293
});
269294

270295
/**
@@ -320,9 +345,9 @@ Object.defineProperty(PIXI.DisplayObject.prototype, 'cacheAsBitmap', {
320345

321346
set: function(value) {
322347

323-
if(this._cacheAsBitmap === value)return;
348+
if (this._cacheAsBitmap === value) return;
324349

325-
if(value)
350+
if (value)
326351
{
327352
this._generateCachedSprite();
328353
}
@@ -351,10 +376,10 @@ PIXI.DisplayObject.prototype.updateTransform = function()
351376
var a, b, c, d, tx, ty;
352377

353378
// so if rotation is between 0 then we can simplify the multiplication process..
354-
if(this.rotation % PIXI.PI_2)
379+
if (this.rotation % PIXI.PI_2)
355380
{
356381
// check to see if the rotation is the same as the previous render. This means we only need to use sin and cos when rotation actually changes
357-
if(this.rotation !== this.rotationCache)
382+
if (this.rotation !== this.rotationCache)
358383
{
359384
this.rotationCache = this.rotation;
360385
this._sr = Math.sin(this.rotation);
@@ -370,7 +395,7 @@ PIXI.DisplayObject.prototype.updateTransform = function()
370395
ty = this.position.y;
371396

372397
// check for pivot.. not often used so geared towards that fact!
373-
if(this.pivot.x || this.pivot.y)
398+
if (this.pivot.x || this.pivot.y)
374399
{
375400
tx -= this.pivot.x * a + this.pivot.y * c;
376401
ty -= this.pivot.x * b + this.pivot.y * d;
@@ -517,14 +542,14 @@ PIXI.DisplayObject.prototype.toGlobal = function(position)
517542
*/
518543
PIXI.DisplayObject.prototype.toLocal = function(position, from)
519544
{
520-
//
521545
if (from)
522546
{
523547
position = from.toGlobal(position);
524548
}
525549

526550
// don't need to u[date the lot
527551
this.displayObjectUpdateTransform();
552+
528553
return this.worldTransform.applyInverse(position);
529554
};
530555

@@ -539,7 +564,7 @@ PIXI.DisplayObject.prototype._renderCachedSprite = function(renderSession)
539564
{
540565
this._cachedSprite.worldAlpha = this.worldAlpha;
541566

542-
if(renderSession.gl)
567+
if (renderSession.gl)
543568
{
544569
PIXI.Sprite.prototype._renderWebGL.call(this._cachedSprite, renderSession);
545570
}
@@ -558,9 +583,10 @@ PIXI.DisplayObject.prototype._renderCachedSprite = function(renderSession)
558583
PIXI.DisplayObject.prototype._generateCachedSprite = function()
559584
{
560585
this._cacheAsBitmap = false;
586+
561587
var bounds = this.getLocalBounds();
562588

563-
if(!this._cachedSprite)
589+
if (!this._cachedSprite)
564590
{
565591
var renderTexture = new PIXI.RenderTexture(bounds.width | 0, bounds.height | 0);//, renderSession.renderer);
566592

@@ -599,7 +625,7 @@ PIXI.DisplayObject.prototype._generateCachedSprite = function()
599625
*/
600626
PIXI.DisplayObject.prototype._destroyCachedSprite = function()
601627
{
602-
if(!this._cachedSprite)return;
628+
if (!this._cachedSprite) return;
603629

604630
this._cachedSprite.texture.destroy(true);
605631

@@ -642,12 +668,15 @@ PIXI.DisplayObject.prototype._renderCanvas = function(renderSession)
642668
* @type Number
643669
*/
644670
Object.defineProperty(PIXI.DisplayObject.prototype, 'x', {
671+
645672
get: function() {
646673
return this.position.x;
647674
},
675+
648676
set: function(value) {
649677
this.position.x = value;
650678
}
679+
651680
});
652681

653682
/**
@@ -657,10 +686,13 @@ Object.defineProperty(PIXI.DisplayObject.prototype, 'x', {
657686
* @type Number
658687
*/
659688
Object.defineProperty(PIXI.DisplayObject.prototype, 'y', {
689+
660690
get: function() {
661691
return this.position.y;
662692
},
693+
663694
set: function(value) {
664695
this.position.y = value;
665696
}
697+
666698
});

src/pixi/display/DisplayObjectContainer.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
PIXI.DisplayObjectContainer = function()
1414
{
15-
PIXI.DisplayObject.call( this );
15+
PIXI.DisplayObject.call(this);
1616

1717
/**
1818
* [read-only] The array of children of this container.
@@ -22,8 +22,6 @@ PIXI.DisplayObjectContainer = function()
2222
* @readOnly
2323
*/
2424
this.children = [];
25-
26-
// fast access to update transform..
2725

2826
};
2927

@@ -38,9 +36,11 @@ PIXI.DisplayObjectContainer.prototype.constructor = PIXI.DisplayObjectContainer;
3836
* @type Number
3937
*/
4038
Object.defineProperty(PIXI.DisplayObjectContainer.prototype, 'width', {
39+
4140
get: function() {
4241
return this.scale.x * this.getLocalBounds().width;
4342
},
43+
4444
set: function(value) {
4545

4646
var width = this.getLocalBounds().width;
@@ -66,14 +66,16 @@ Object.defineProperty(PIXI.DisplayObjectContainer.prototype, 'width', {
6666
* @type Number
6767
*/
6868
Object.defineProperty(PIXI.DisplayObjectContainer.prototype, 'height', {
69+
6970
get: function() {
7071
return this.scale.y * this.getLocalBounds().height;
7172
},
73+
7274
set: function(value) {
7375

7476
var height = this.getLocalBounds().height;
7577

76-
if(height !== 0)
78+
if (height !== 0)
7779
{
7880
this.scale.y = value / height ;
7981
}
@@ -84,6 +86,7 @@ Object.defineProperty(PIXI.DisplayObjectContainer.prototype, 'height', {
8486

8587
this._height = value;
8688
}
89+
8790
});
8891

8992
/**

0 commit comments

Comments
 (0)