Skip to content

Commit a802914

Browse files
committed
Added in destroy methods for all managers and invoked them from Game
1 parent f1a2592 commit a802914

22 files changed

Lines changed: 200 additions & 38 deletions

src/animations/AnimationManager.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ var AnimationManager = new Class({
8484
boot: function ()
8585
{
8686
this.textureManager = this.game.textures;
87+
88+
this.game.events.once('destroy', this.destroy, this);
8789
},
8890

8991
/**
@@ -551,7 +553,11 @@ var AnimationManager = new Class({
551553
*/
552554
destroy: function ()
553555
{
554-
// TODO
556+
this.anims.clear();
557+
558+
this.textureManager = null;
559+
560+
this.game = null;
555561
}
556562

557563
});

src/boot/Game.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
var AddToDOM = require('../dom/AddToDOM');
22
var AnimationManager = require('../animations/AnimationManager');
33
var CacheManager = require('../cache/CacheManager');
4+
var CanvasPool = require('../display/canvas/CanvasPool');
45
var Class = require('../utils/Class');
56
var Config = require('./Config');
67
var CreateRenderer = require('./CreateRenderer');
7-
var Data = require('../data/DataManager');
8+
var DataManager = require('../data/DataManager');
89
var DebugHeader = require('./DebugHeader');
910
var Device = require('../device');
1011
var DOMContentLoaded = require('../dom/DOMContentLoaded');
@@ -133,10 +134,10 @@ var Game = new Class({
133134
/**
134135
* [description]
135136
*
136-
* @property {Phaser.Data} registry
137+
* @property {Phaser.Data.DataManager} registry
137138
* @since 3.0.0
138139
*/
139-
this.registry = new Data(this);
140+
this.registry = new DataManager(this);
140141

141142
/**
142143
* An instance of the Input Manager.
@@ -212,9 +213,6 @@ var Game = new Class({
212213

213214
// Wait for the DOM Ready event, then call boot.
214215
DOMContentLoaded(this.boot.bind(this));
215-
216-
// For debugging only
217-
window.game = this;
218216
},
219217

220218
/**
@@ -425,9 +423,24 @@ var Game = new Class({
425423
* @method Phaser.Game#destroy
426424
* @since 3.0.0
427425
*/
428-
destroy: function ()
426+
destroy: function (removeCanvas)
429427
{
430-
// TODO
428+
this.loop.destroy();
429+
430+
this.scene.destroy();
431+
432+
this.renderer.destroy();
433+
434+
this.events.emit('destroy');
435+
436+
this.events.removeAllListeners();
437+
438+
this.onStepCallback = null;
439+
440+
if (removeCanvas)
441+
{
442+
CanvasPool.remove(this.canvas);
443+
}
431444
}
432445

433446
});

src/boot/TimeStep.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,8 @@ var TimeStep = new Class({
598598
{
599599
this.stop();
600600

601-
this.callback = null;
601+
this.callback = NOOP;
602+
602603
this.raf = null;
603604
this.game = null;
604605
}

src/cache/CacheManager.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ var CacheManager = new Class({
131131
* @since 3.0.0
132132
*/
133133
this.custom = {};
134+
135+
this.game.events.once('destroy', this.destroy, this);
134136
},
135137

136138
/**

src/data/DataManager.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var DataManager = new Class({
2727
this.blockSet = false;
2828

2929
this._frozen = false;
30+
31+
this.events.once('destroy', this.destroy, this);
3032
},
3133

3234
// Retrieves the value for the given key, or undefined if it doesn't exist.

src/dom/DOMContentLoaded.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,26 @@
11
var OS = require('../device/OS');
22

3-
var isBooted = false;
4-
53
/**
64
* Inspects the readyState of the document. If the document is already complete then it invokes the given callback.
75
* If not complete it sets up several event listeners such as `deviceready`, and once those fire, it invokes the callback.
8-
* Called automatically by the Phaser.Game instance. Should not usually be access directly.
6+
* Called automatically by the Phaser.Game instance. Should not usually be accessed directly.
97
*
108
* @function Phaser.Dom.DOMContentLoaded
119
* @since 3.0.0
1210
*
1311
* @param {function} callback - The callback to be invoked when the device is ready and the DOM content is loaded.
14-
*
15-
* @return {boolean} Returns `false` if the document is already loaded, otherwise `true` if the callback is pending.
1612
*/
1713
var DOMContentLoaded = function (callback)
1814
{
19-
if (isBooted)
20-
{
21-
return false;
22-
}
23-
2415
if (document.readyState === 'complete' || document.readyState === 'interactive')
2516
{
26-
isBooted = true;
27-
2817
callback();
2918

30-
return true;
19+
return;
3120
}
3221

3322
var check = function ()
3423
{
35-
isBooted = true;
36-
3724
document.removeEventListener('deviceready', check, true);
3825
document.removeEventListener('DOMContentLoaded', check, true);
3926
window.removeEventListener('load', check, true);
@@ -55,8 +42,6 @@ var DOMContentLoaded = function (callback)
5542
document.addEventListener('DOMContentLoaded', check, true);
5643
window.addEventListener('load', check, true);
5744
}
58-
59-
return true;
6045
};
6146

6247
module.exports = DOMContentLoaded;

src/input/InputManager.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ var InputManager = new Class({
193193
this.mouse.boot();
194194
this.touch.boot();
195195
this.gamepad.boot();
196+
197+
this.game.events.once('destroy', this.destroy, this);
196198
},
197199

198200
/**
@@ -500,6 +502,28 @@ var InputManager = new Class({
500502
getScaleY: function ()
501503
{
502504
return this.game.config.height / this.bounds.height;
505+
},
506+
507+
/**
508+
* [description]
509+
*
510+
* @method Phaser.Input.InputManager#destroy
511+
* @since 3.0.0
512+
*/
513+
destroy: function ()
514+
{
515+
this.events.removeAllListeners();
516+
517+
this.keyboard.destroy();
518+
this.mouse.destroy();
519+
this.touch.destroy();
520+
this.gamepad.destroy();
521+
522+
this.activePointer.destroy();
523+
524+
this.queue = [];
525+
526+
this.game = null;
503527
}
504528

505529
});

src/input/Pointer.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,19 @@ var Pointer = new Class({
584584
forwardButtonDown: function ()
585585
{
586586
return (this.buttons & 16);
587+
},
588+
589+
/**
590+
* [description]
591+
*
592+
* @method Phaser.Input.Pointer#destroy
593+
* @since 3.0.0
594+
*/
595+
destroy: function ()
596+
{
597+
this.camera = null;
598+
this.manager = null;
599+
this.position = null;
587600
}
588601

589602
});

src/input/gamepad/GamepadManager.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,20 @@ var GamepadManager = new Class({
339339
}
340340
},
341341

342+
/**
343+
* [description]
344+
*
345+
* @method Phaser.Input.Gamepad.GamepadManager#destroy
346+
* @since 3.0.0
347+
*/
348+
destroy: function ()
349+
{
350+
this.stopListeners();
351+
this.disconnectAll();
352+
353+
this.gamepads = [];
354+
},
355+
342356
/**
343357
* The total number of connected game pads.
344358
*

src/input/keyboard/KeyboardManager.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ var KeyboardManager = new Class({
402402
this.captures = [];
403403
this.queue = [];
404404
this.handler = undefined;
405+
406+
this.manager = null;
405407
}
406408

407409
});

0 commit comments

Comments
 (0)