Skip to content

Commit 3d0be3e

Browse files
committed
Group.remove and clear have optional destroyChild arguments
1 parent e14d0c5 commit 3d0be3e

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* Group.getLast will return the last member in the Group matching the search criteria.
88
* Group.getFirstNth will return the nth member in the Group, scanning from top to bottom, that matches the search criteria.
99
* Group.getLastNth will return the nth member in the Group, scanning in reverse, that matches the search criteria.
10+
* Group.remove has a new optional argument `destroyChild` that will call `destroy` on the child after removing it.
11+
* Group.clear has a new optional argument `destroyChild` that will call `destroy` on all children in the Group after removing them.
1012

1113
### Updates
1214

@@ -16,6 +18,7 @@
1618
* Impact Physics Game Objects have changed `setActive` to `setActiveCollision`, previously the `setActive` collision method was overwriting the Game Objects `setActive` method, hence the renaming.
1719
* The modifications made to the RTree class in Phaser 3.4.0 to avoid CSP policy violations caused a significant performance hit once a substantial number of bodies were involved. We have recoded how the class deals with its accessor formats and returned to 3.3 level performance while still maintaining CSP policy adherence. Fix #3594 (thanks @16patsle)
1820
* The Retro Font namespace has changed to `Phaser.GameObjects.RetroFont`. Previously, you would access the parser and constants via `BitmapText`, i.e.: `Phaser.GameObjects.BitmapText.ParseRetroFont.TEXT_SET6`. This has now changed to its own namespace, so the same line would be: `Phaser.GameObjects.RetroFont.TEXT_SET6`. The Parser is available via `Phaser.GameObjects.RetroFont.Parse`. This keeps things cleaner and also unbinds RetroFont from BitmapText, allowing you to cleanly exclude it from your build should you wish. All examples have been updated to reflect this.
21+
* If using the `removeFromScene` option in Group.remove or Group.clear it will remove the child/ren from the Scene to which they belong, not the Scene the Group belongs to.
1922

2023
### Bug Fixes
2124

src/gameobjects/group/Group.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -561,21 +561,23 @@ var Group = new Class({
561561
},
562562

563563
/**
564-
* Removes a member of this group.
564+
* Removes a member of this Group and optionally removes it from the Scene and / or destroys it.
565565
*
566566
* Calls {@link Phaser.GameObjects.Group#removeCallback}.
567567
*
568568
* @method Phaser.GameObjects.Group#remove
569569
* @since 3.0.0
570570
*
571571
* @param {Phaser.GameObjects.GameObject} child - The Game Object to remove.
572-
* @param {boolean} [removeFromScene=false] - Also remove the group member from the scene.
572+
* @param {boolean} [removeFromScene=false] - Optionally remove the Group member from the Scene it belongs to.
573+
* @param {boolean} [destroyChild=false] - Optionally call destroy on the removed Group member.
573574
*
574575
* @return {Phaser.GameObjects.Group} This Group object.
575576
*/
576-
remove: function (child, removeFromScene)
577+
remove: function (child, removeFromScene, destroyChild)
577578
{
578579
if (removeFromScene === undefined) { removeFromScene = false; }
580+
if (destroyChild === undefined) { destroyChild = false; }
579581

580582
if (!this.children.contains(child))
581583
{
@@ -589,36 +591,42 @@ var Group = new Class({
589591
this.removeCallback.call(this, child);
590592
}
591593

592-
if (removeFromScene)
594+
child.off('destroy', this.remove, this);
595+
596+
if (destroyChild)
593597
{
594-
this.scene.sys.displayList.remove(child);
598+
child.destroy();
599+
}
600+
else if (removeFromScene)
601+
{
602+
child.scene.sys.displayList.remove(child);
595603

596604
if (child.preUpdate)
597605
{
598-
this.scene.sys.updateList.remove(child);
606+
child.scene.sys.updateList.remove(child);
599607
}
600608
}
601609

602-
child.off('destroy', this.remove, this);
603-
604610
return this;
605611
},
606612

607613
/**
608-
* Removes all members of this group.
614+
* Removes all members of this Group and optionally removes them from the Scene and / or destroys them.
609615
*
610616
* Does not call {@link Phaser.GameObjects.Group#removeCallback}.
611617
*
612618
* @method Phaser.GameObjects.Group#clear
613619
* @since 3.0.0
614620
*
615-
* @param {boolean} [removeFromScene=false] - Also remove each group member from the scene.
621+
* @param {boolean} [removeFromScene=false] - Optionally remove each Group member from the Scene.
622+
* @param {boolean} [destroyChild=false] - Optionally call destroy on the removed Group members.
616623
*
617624
* @return {Phaser.GameObjects.Group} This group.
618625
*/
619-
clear: function (removeFromScene)
626+
clear: function (removeFromScene, destroyChild)
620627
{
621628
if (removeFromScene === undefined) { removeFromScene = false; }
629+
if (destroyChild === undefined) { destroyChild = false; }
622630

623631
var children = this.children;
624632

@@ -628,13 +636,17 @@ var Group = new Class({
628636

629637
gameObject.off('destroy', this.remove, this);
630638

631-
if (removeFromScene)
639+
if (destroyChild)
640+
{
641+
gameObject.destroy();
642+
}
643+
else if (removeFromScene)
632644
{
633-
this.scene.sys.displayList.remove(gameObject);
645+
gameObject.scene.sys.displayList.remove(gameObject);
634646

635647
if (gameObject.preUpdate)
636648
{
637-
this.scene.sys.updateList.remove(gameObject);
649+
gameObject.scene.sys.updateList.remove(gameObject);
638650
}
639651
}
640652
}

0 commit comments

Comments
 (0)