From 8f061e103997f9a3147c686e7c39755b01cb61f9 Mon Sep 17 00:00:00 2001 From: Rex Date: Thu, 21 Sep 2023 00:40:53 +0800 Subject: [PATCH] Update remove, removeAll, add methods --- src/gameobjects/layer/Layer.js | 102 +++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/src/gameobjects/layer/Layer.js b/src/gameobjects/layer/Layer.js index ef6641be8..b24e82490 100644 --- a/src/gameobjects/layer/Layer.js +++ b/src/gameobjects/layer/Layer.js @@ -604,6 +604,104 @@ var Layer = new Class({ return this; }, + /** + * Adds the given Game Object, or array of Game Objects, to this Layer. + * + * Each Game Object must be unique within the Layer. + * + * @method Phaser.GameObjects.Layer#add + * @since 3.4.0 + * + * @generic {Phaser.GameObjects.GameObject} T + * @genericUse {(T|T[])} - [child] + * + * @param {Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]} child - The Game Object, or array of Game Objects, to add to the Layer. + * + * @return {this} This Layer instance. + */ + add: function(child) + { + List.prototype.add.call(this, child); + + return this; + }, + + /** + * Removes the given Game Object, or array of Game Objects, from this Layer. + * + * The Game Objects must already be children of this Layer. + * + * You can also optionally call `destroy` on each Game Object that is removed from the Layer. + * + * @method Phaser.GameObjects.Layer#remove + * @since 3.61.0 + * + * @generic {Phaser.GameObjects.GameObject} T + * @genericUse {(T|T[])} - [child] + * + * @param {Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]} child - The Game Object, or array of Game Objects, to be removed from the Layer. + * @param {boolean} [destroyChild=false] - Optionally call `destroy` on each child successfully removed from this Layer. + * + * @return {this} This Layer instance. + */ + remove: function (child, destroyChild) + { + var removed = List.prototype.remove.call(this, child); + + if (destroyChild && removed) + { + if (!Array.isArray(removed)) + { + removed = [ removed ]; + } + + for (var i = 0; i < removed.length; i++) + { + removed[i].destroy(); + } + } + + return this; + }, + + /** + * Removes all Game Objects from this Layer. + * + * You can also optionally call `destroy` on each Game Object that is removed from the Layer. + * + * @method Phaser.GameObjects.Layer#removeAll + * @since 3.4.0 + * + * @param {boolean} [destroyChild=false] - Optionally call `destroy` on each Game Object successfully removed from this Layer. + * + * @return {this} This Layer instance. + */ + removeAll: function (destroyChild) + { + var list = this.list; + + if (destroyChild) + { + for (var i = 0; i < list.length; i++) + { + if (list[i] && list[i].scene) + { + list[i].off(GameObjectEvents.DESTROY, this.remove, this); + + list[i].destroy(); + } + } + + this.list = []; + } + else + { + ArrayUtils.RemoveBetween(list, 0, list.length, this.removeChildCallback, this); + } + + return this; + }, + /** * This callback is invoked when this Game Object is added to a Scene. * @@ -730,6 +828,8 @@ var Layer = new Class({ */ addChildCallback: function (gameObject) { + gameObject.once(GameObjectEvents.DESTROY, this.remove, this); + if (gameObject.displayList && gameObject.displayList !== this) { gameObject.removeFromDisplayList(); @@ -760,6 +860,8 @@ var Layer = new Class({ */ removeChildCallback: function (gameObject) { + gameObject.off(GameObjectEvents.DESTROY, this.remove, this); + this.queueDepthSort(); gameObject.displayList = null;