Skip to content

Commit 98930de

Browse files
committed
The SceneManager has a new method: remove which allows you to remove and destroy a Scene, freeing up the Scene key for use by future scenes and potentially clearing the Scene from active memory for gc.
1 parent 1f8d0c8 commit 98930de

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* The Headless render mode has been implemented. You can now set HEADLESS as the `renderType` in the Game Config and it will run a special game step that skips rendering. It will still create a Canvas element, as lots of internal systems (like input) rely on it, but it will not draw anything to it. Fix #3256 (thanks @rgk)
1313
* GameObject.setInteractive has a new boolean argument `dropZone` which will allow you to set the object as being a drop zone right from the method.
1414
* Sprites can now be drop zones and have other Game Objects dragged onto them as targets.
15+
* The SceneManager has a new method: `remove` which allows you to remove and destroy a Scene, freeing up the Scene key for use by future scenes and potentially clearing the Scene from active memory for gc.
1516

1617
### Bug Fixes
1718

src/scene/SceneManager.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,58 @@ var SceneManager = new Class({
257257
return newScene;
258258
},
259259

260+
/**
261+
* Removes a Scene from the SceneManager.
262+
*
263+
* The Scene is removed from the local scenes array, it's key is cleared from the keys
264+
* cache and Scene.Systems.destroy is then called on it.
265+
*
266+
* If the SceneManager is processing the Scenes when this method is called it wil
267+
* queue the operation for the next update sequence.
268+
*
269+
* @method Phaser.Scenes.SceneManager#remove
270+
* @since 3.2.0
271+
*
272+
* @param {string|Phaser.Scene} scene - The Scene to be removed.
273+
*
274+
* @return {Phaser.Scenes.SceneManager} This SceneManager.
275+
*/
276+
remove: function (key)
277+
{
278+
if (this._processing)
279+
{
280+
this._queue.push({ op: 'remove', keyA: key, keyB: null });
281+
}
282+
else
283+
{
284+
var sceneToRemove = this.getScene(key);
285+
286+
if (!sceneToRemove)
287+
{
288+
return this;
289+
}
290+
291+
var index = this.scenes.indexOf(sceneToRemove);
292+
var sceneKey = sceneToRemove.sys.settings.key;
293+
294+
if (index > -1)
295+
{
296+
this.keys[sceneKey] = undefined;
297+
this.scenes.splice(index, 1);
298+
299+
if (this._start.indexOf(sceneKey) > -1)
300+
{
301+
index = this._start.indexOf(sceneKey);
302+
this._start.splice(index, 1);
303+
}
304+
305+
sceneToRemove.sys.destroy();
306+
}
307+
}
308+
309+
return this;
310+
},
311+
260312
/**
261313
* [description]
262314
*

src/scene/ScenePlugin.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,31 @@ var ScenePlugin = new Class({
396396
return this;
397397
},
398398

399+
/**
400+
* Removes a Scene from the SceneManager.
401+
*
402+
* The Scene is removed from the local scenes array, it's key is cleared from the keys
403+
* cache and Scene.Systems.destroy is then called on it.
404+
*
405+
* If the SceneManager is processing the Scenes when this method is called it wil
406+
* queue the operation for the next update sequence.
407+
*
408+
* @method Phaser.Scenes.ScenePlugin#remove
409+
* @since 3.2.0
410+
*
411+
* @param {string|Phaser.Scene} scene - The Scene to be removed.
412+
*
413+
* @return {Phaser.Scenes.SceneManager} This SceneManager.
414+
*/
415+
remove: function (key)
416+
{
417+
if (key === undefined) { key = this.key; }
418+
419+
this.manager.remove(key);
420+
421+
return this;
422+
},
423+
399424
/**
400425
* [description]
401426
*

0 commit comments

Comments
 (0)