Skip to content

Commit 2916f04

Browse files
committed
Group.bringToTop (and consequently Sprite.bringToTop) no longer removes the child from the InputManager if enabled (thanks @BinaryMoon, fix phaserjs#928)
Group.sendToBack (and consequently Sprite.sendToBack) no longer removes the child from the InputManager if enabled. Group.add has a new optional boolean parameter: `silent`. If set to `true` the child will not dispatch its `onAddedToGroup` event. Group.addAt has a new optional boolean parameter: `silent`. If set to `true` the child will not dispatch its `onAddedToGroup` event. Group.remove has a new optional boolean parameter: `silent`. If set to `true` the child will not dispatch its `onRemovedFromGroup` event. Group.removeBetween has a new optional boolean parameter: `silent`. If set to `true` the children will not dispatch their `onRemovedFromGroup` events. Group.removeAll has a new optional boolean parameter: `silent`. If set to `true` the children will not dispatch their `onRemovedFromGroup` events. Internal child movements in Group (such as bringToTop) now uses the new `silent` parameter to avoid the child emitting incorrect Group addition and deletion events.
1 parent 2cef655 commit 2916f04

3 files changed

Lines changed: 41 additions & 21 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ Version 2.0.6 - "Jornhill" - -in development-
115115
* Full Mouse Wheel support added, with new constants and callbacks for mouse wheel movement (thanks @woutercommandeur, #959)
116116
* A Phaser version of the Pixi PixelateFilter was added by @paperkettle #939)
117117
* TileMap.setPreventRecalculate allows you to turn on / off the recalculation of tile faces for tile collision, which is handy when modifying large portions of a map to avoid slow-down (thanks @sivael, #951)
118+
* Group.add has a new optional boolean parameter: `silent`. If set to `true` the child will not dispatch its `onAddedToGroup` event.
119+
* Group.addAt has a new optional boolean parameter: `silent`. If set to `true` the child will not dispatch its `onAddedToGroup` event.
120+
* Group.remove has a new optional boolean parameter: `silent`. If set to `true` the child will not dispatch its `onRemovedFromGroup` event.
121+
* Group.removeBetween has a new optional boolean parameter: `silent`. If set to `true` the children will not dispatch their `onRemovedFromGroup` events.
122+
* Group.removeAll has a new optional boolean parameter: `silent`. If set to `true` the children will not dispatch their `onRemovedFromGroup` events.
123+
* Internal child movements in Group (such as bringToTop) now uses the new `silent` parameter to avoid the child emitting incorrect Group addition and deletion events.
118124

119125

120126
### Bug Fixes
@@ -148,6 +154,8 @@ Calling addToWorld() would previously not check the _toRemove array, which could
148154
* Device.mobileSafari was no longer detecting Mobile Safari, now fixed (thanks @Zammy, #927)
149155
* Rectangle.right when set would set the new width to be Rectangle.x + the value given. However the value given should be a new Right coordinate, so the width calculation has been adjusted to compensate (thanks @cryptonomicon, #849)
150156
* Calling Tween.stop from inside a Tween update callback would still cause the tween onComplete event to fire (thanks @eguneys, #924)
157+
* Group.bringToTop (and consequently Sprite.bringToTop) no longer removes the child from the InputManager if enabled (thanks @BinaryMoon, fix #928)
158+
* Group.sendToBack (and consequently Sprite.sendToBack) no longer removes the child from the InputManager if enabled.
151159

152160

153161
### ToDo

build/phaser.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,10 +2253,10 @@ declare module Phaser {
22532253
type: number;
22542254
z: number;
22552255

2256-
add(child: any): any;
2256+
add(child: any, silent?: boolean): any;
22572257
addAll(property: string, amount: number, checkAlive: boolean, checkVisible: boolean): void;
2258+
addAt(child: any, index: number, silent?: boolean): any;
22582259
bringToTop(child: any): any;
2259-
addAt(child: any, index: number): any;
22602260
callAll(method: string, context: any, ...parameters: any[]): void;
22612261
callAllExists(callback: Function, existsValue: boolean, ...parameters: any[]): void;
22622262
callbackFromArray(child: Object, callback: Function, length: number): void;
@@ -2288,9 +2288,9 @@ declare module Phaser {
22882288
postUpdate(): void;
22892289
preUpdate(): void;
22902290
previous(): void;
2291-
remove(child: any, destroy?: boolean): boolean;
2292-
removeAll(destroy?: boolean): void;
2293-
removeBetween(startIndex: number, endIndex?: number, destroy?: boolean): void;
2291+
remove(child: any, destroy?: boolean, silent?: boolean): boolean;
2292+
removeAll(destroy?: boolean, silent?: boolean): void;
2293+
removeBetween(startIndex: number, endIndex?: number, destroy?: boolean, silent?: boolean): void;
22942294
replace(oldChild: any, newChild: any): any;
22952295
reverse(): void;
22962296
sendToBack(child: any): any;

src/core/Group.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,12 @@ Phaser.Group.SORT_DESCENDING = 1;
190190
* @see Phaser.Group#addAt
191191
* @method Phaser.Group#add
192192
* @param {*} child - An instance of Phaser.Sprite, Phaser.Button or any other display object..
193+
* @param {boolean} [silent=false] - If the silent parameter is `true` the child will not dispatch the onAddedToGroup event.
193194
* @return {*} The child that was added to the Group.
194195
*/
195-
Phaser.Group.prototype.add = function (child) {
196+
Phaser.Group.prototype.add = function (child, silent) {
197+
198+
if (typeof silent === 'undefined') { silent = false; }
196199

197200
if (child.parent !== this)
198201
{
@@ -205,7 +208,7 @@ Phaser.Group.prototype.add = function (child) {
205208

206209
child.z = this.children.length;
207210

208-
if (child.events)
211+
if (!silent && child.events)
209212
{
210213
child.events.onAddedToGroup.dispatch(child, this);
211214
}
@@ -227,9 +230,12 @@ Phaser.Group.prototype.add = function (child) {
227230
* @method Phaser.Group#addAt
228231
* @param {*} child - An instance of Phaser.Sprite, Phaser.Button or any other display object..
229232
* @param {number} index - The index within the Group to insert the child to.
233+
* @param {boolean} [silent=false] - If the silent parameter is `true` the child will not dispatch the onAddedToGroup event.
230234
* @return {*} The child that was added to the Group.
231235
*/
232-
Phaser.Group.prototype.addAt = function (child, index) {
236+
Phaser.Group.prototype.addAt = function (child, index, silent) {
237+
238+
if (typeof silent === 'undefined') { silent = false; }
233239

234240
if (child.parent !== this)
235241
{
@@ -242,7 +248,7 @@ Phaser.Group.prototype.addAt = function (child, index) {
242248

243249
this.updateZ();
244250

245-
if (child.events)
251+
if (!silent && child.events)
246252
{
247253
child.events.onAddedToGroup.dispatch(child, this);
248254
}
@@ -473,8 +479,8 @@ Phaser.Group.prototype.bringToTop = function (child) {
473479

474480
if (child.parent === this && this.getIndex(child) < this.children.length)
475481
{
476-
this.remove(child);
477-
this.add(child);
482+
this.remove(child, false, true);
483+
this.add(child, true);
478484
}
479485

480486
return child;
@@ -492,8 +498,8 @@ Phaser.Group.prototype.sendToBack = function (child) {
492498

493499
if (child.parent === this && this.getIndex(child) > 0)
494500
{
495-
this.remove(child);
496-
this.addAt(child, 0);
501+
this.remove(child, false, true);
502+
this.addAt(child, 0, true);
497503
}
498504

499505
return child;
@@ -1486,18 +1492,20 @@ Phaser.Group.prototype.getRandom = function (startIndex, length) {
14861492
* @method Phaser.Group#remove
14871493
* @param {Any} child - The child to remove.
14881494
* @param {boolean} [destroy=false] - You can optionally call destroy on the child that was removed.
1495+
* @param {boolean} [silent=false] - If the silent parameter is `true` the child will not dispatch the onRemovedFromGroup event.
14891496
* @return {boolean} true if the child was removed from this Group, otherwise false.
14901497
*/
1491-
Phaser.Group.prototype.remove = function (child, destroy) {
1498+
Phaser.Group.prototype.remove = function (child, destroy, silent) {
14921499

14931500
if (typeof destroy === 'undefined') { destroy = false; }
1501+
if (typeof silent === 'undefined') { silent = false; }
14941502

14951503
if (this.children.length === 0 || this.children.indexOf(child) === -1)
14961504
{
14971505
return false;
14981506
}
14991507

1500-
if (child.events && !child.destroyPhase)
1508+
if (!silent && child.events && !child.destroyPhase)
15011509
{
15021510
child.events.onRemovedFromGroup.dispatch(child, this);
15031511
}
@@ -1521,15 +1529,17 @@ Phaser.Group.prototype.remove = function (child, destroy) {
15211529
};
15221530

15231531
/**
1524-
* Removes all children from this Group, setting all group properties to null.
1532+
* Removes all children from this Group, setting the group properties of the children to `null`.
15251533
* The Group container remains on the display list.
15261534
*
15271535
* @method Phaser.Group#removeAll
1528-
* @param {boolean} [destroy=false] - You can optionally call destroy on the child that was removed.
1536+
* @param {boolean} [destroy=false] - You can optionally call destroy on each child that is removed.
1537+
* @param {boolean} [silent=false] - If the silent parameter is `true` the children will not dispatch their onRemovedFromGroup events.
15291538
*/
1530-
Phaser.Group.prototype.removeAll = function (destroy) {
1539+
Phaser.Group.prototype.removeAll = function (destroy, silent) {
15311540

15321541
if (typeof destroy === 'undefined') { destroy = false; }
1542+
if (typeof silent === 'undefined') { silent = false; }
15331543

15341544
if (this.children.length === 0)
15351545
{
@@ -1538,7 +1548,7 @@ Phaser.Group.prototype.removeAll = function (destroy) {
15381548

15391549
do
15401550
{
1541-
if (this.children[0].events)
1551+
if (!silent && this.children[0].events)
15421552
{
15431553
this.children[0].events.onRemovedFromGroup.dispatch(this.children[0], this);
15441554
}
@@ -1563,11 +1573,13 @@ Phaser.Group.prototype.removeAll = function (destroy) {
15631573
* @param {number} startIndex - The index to start removing children from.
15641574
* @param {number} [endIndex] - The index to stop removing children at. Must be higher than startIndex. If undefined this method will remove all children between startIndex and the end of the Group.
15651575
* @param {boolean} [destroy=false] - You can optionally call destroy on the child that was removed.
1576+
* @param {boolean} [silent=false] - If the silent parameter is `true` the children will not dispatch their onRemovedFromGroup events.
15661577
*/
1567-
Phaser.Group.prototype.removeBetween = function (startIndex, endIndex, destroy) {
1578+
Phaser.Group.prototype.removeBetween = function (startIndex, endIndex, destroy, silent) {
15681579

15691580
if (typeof endIndex === 'undefined') { endIndex = this.children.length; }
15701581
if (typeof destroy === 'undefined') { destroy = false; }
1582+
if (typeof silent === 'undefined') { silent = false; }
15711583

15721584
if (this.children.length === 0)
15731585
{
@@ -1583,7 +1595,7 @@ Phaser.Group.prototype.removeBetween = function (startIndex, endIndex, destroy)
15831595

15841596
while (i >= startIndex)
15851597
{
1586-
if (this.children[i].events)
1598+
if (!silent && this.children[i].events)
15871599
{
15881600
this.children[i].events.onRemovedFromGroup.dispatch(this.children[i], this);
15891601
}

0 commit comments

Comments
 (0)