forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroup.js
More file actions
2305 lines (1904 loc) · 73.7 KB
/
Copy pathGroup.js
File metadata and controls
2305 lines (1904 loc) · 73.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2015 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* A Group is a container for {@link DisplayObject display objects} including {@link Phaser.Sprite Sprites} and {@link Phaser.Image Images}.
*
* Groups form the logical tree structure of the display/scene graph where local transformations are applied to children.
* For instance, all children are also moved/rotated/scaled when the group is moved/rotated/scaled.
*
* In addition, Groups provides support for fast pooling and object recycling.
*
* Groups are also display objects and can be nested as children within other Groups.
*
* @class Phaser.Group
* @extends PIXI.DisplayObjectContainer
* @param {Phaser.Game} game - A reference to the currently running game.
* @param {DisplayObject|null} [parent=(game world)] - The parent Group (or other {@link DisplayObject}) that this group will be added to.
* If undefined/unspecified the Group will be added to the {@link Phaser.Game#world Game World}; if null the Group will not be added to any parent.
* @param {string} [name='group'] - A name for this group. Not used internally but useful for debugging.
* @param {boolean} [addToStage=false] - If true this group will be added directly to the Game.Stage instead of Game.World.
* @param {boolean} [enableBody=false] - If true all Sprites created with {@link #create} or {@link #createMulitple} will have a physics body created on them. Change the body type with {@link #physicsBodyType}.
* @param {integer} [physicsBodyType=0] - The physics body type to use when physics bodies are automatically added. See {@link #physicsBodyType} for values.
*/
Phaser.Group = function (game, parent, name, addToStage, enableBody, physicsBodyType) {
if (addToStage === undefined) { addToStage = false; }
if (enableBody === undefined) { enableBody = false; }
if (physicsBodyType === undefined) { physicsBodyType = Phaser.Physics.ARCADE; }
/**
* A reference to the currently running Game.
* @property {Phaser.Game} game
* @protected
*/
this.game = game;
if (parent === undefined)
{
parent = game.world;
}
/**
* A name for this group. Not used internally but useful for debugging.
* @property {string} name
*/
this.name = name || 'group';
/**
* The z-depth value of this object within its parent container/Group - the World is a Group as well.
* This value must be unique for each child in a Group.
* @property {integer} z
* @readOnly
*/
this.z = 0;
PIXI.DisplayObjectContainer.call(this);
if (addToStage)
{
this.game.stage.addChild(this);
this.z = this.game.stage.children.length;
}
else
{
if (parent)
{
parent.addChild(this);
this.z = parent.children.length;
}
}
/**
* Internal Phaser Type value.
* @property {integer} type
* @protected
*/
this.type = Phaser.GROUP;
/**
* @property {number} physicsType - The const physics body type of this object.
* @readonly
*/
this.physicsType = Phaser.GROUP;
/**
* The alive property is useful for Groups that are children of other Groups and need to be included/excluded in checks like forEachAlive.
* @property {boolean} alive
* @default
*/
this.alive = true;
/**
* If exists is true the group is updated, otherwise it is skipped.
* @property {boolean} exists
* @default
*/
this.exists = true;
/**
* A group with `ignoreDestroy` set to `true` ignores all calls to its `destroy` method.
* @property {boolean} ignoreDestroy
* @default
*/
this.ignoreDestroy = false;
/**
* A Group is that has `pendingDestroy` set to `true` is flagged to have its destroy method
* called on the next logic update.
* You can set it directly to flag the Group to be destroyed on its next update.
*
* This is extremely useful if you wish to destroy a Group from within one of its own callbacks
* or a callback of one of its children.
*
* @property {boolean} pendingDestroy
*/
this.pendingDestroy = false;
/**
* The type of objects that will be created when using {@link #create} or {@link #createMultiple}.
*
* Any object may be used but it should extend either Sprite or Image and accept the same constructor arguments:
* when a new object is created it is passed the following parameters to its constructor: `(game, x, y, key, frame)`.
*
* @property {object} classType
* @default {@link Phaser.Sprite}
*/
this.classType = Phaser.Sprite;
/**
* The current display object that the group cursor is pointing to, if any. (Can be set manually.)
*
* The cursor is a way to iterate through the children in a Group using {@link #next} and {@link #previous}.
* @property {?DisplayObject} cursor
*/
this.cursor = null;
/**
* If true all Sprites created by, or added to this group, will have a physics body enabled on them.
*
* The default body type is controlled with {@link #physicsBodyType}.
* @property {boolean} enableBody
*/
this.enableBody = enableBody;
/**
* If true when a physics body is created (via {@link #enableBody}) it will create a physics debug object as well.
*
* This only works for P2 bodies.
* @property {boolean} enableBodyDebug
* @default
*/
this.enableBodyDebug = false;
/**
* If {@link #enableBody} is true this is the type of physics body that is created on new Sprites.
*
* The valid values are {@link Phaser.Physics.ARCADE}, {@link Phaser.Physics.P2JS}, {@link Phaser.Physics.NINJA}, etc.
* @property {integer} physicsBodyType
*/
this.physicsBodyType = physicsBodyType;
/**
* If this Group contains Arcade Physics Sprites you can set a custom sort direction via this property.
*
* It should be set to one of the Phaser.Physics.Arcade sort direction constants:
*
* Phaser.Physics.Arcade.SORT_NONE
* Phaser.Physics.Arcade.LEFT_RIGHT
* Phaser.Physics.Arcade.RIGHT_LEFT
* Phaser.Physics.Arcade.TOP_BOTTOM
* Phaser.Physics.Arcade.BOTTOM_TOP
*
* If set to `null` the Group will use whatever Phaser.Physics.Arcade.sortDirection is set to. This is the default behavior.
*
* @property {integer} physicsSortDirection
* @default
*/
this.physicsSortDirection = null;
/**
* This signal is dispatched when the group is destroyed.
* @property {Phaser.Signal} onDestroy
*/
this.onDestroy = new Phaser.Signal();
/**
* @property {integer} cursorIndex - The current index of the Group cursor. Advance it with Group.next.
* @readOnly
*/
this.cursorIndex = 0;
/**
* A Group that is fixed to the camera uses its x/y coordinates as offsets from the top left of the camera. These are stored in Group.cameraOffset.
*
* Note that the cameraOffset values are in addition to any parent in the display list.
* So if this Group was in a Group that has x: 200, then this will be added to the cameraOffset.x
*
* @property {boolean} fixedToCamera
*/
this.fixedToCamera = false;
/**
* If this object is {@link #fixedToCamera} then this stores the x/y position offset relative to the top-left of the camera view.
* If the parent of this Group is also `fixedToCamera` then the offset here is in addition to that and should typically be disabled.
* @property {Phaser.Point} cameraOffset
*/
this.cameraOffset = new Phaser.Point();
/**
* The hash array is an array belonging to this Group into which you can add any of its children via Group.addToHash and Group.removeFromHash.
*
* Only children of this Group can be added to and removed from the hash.
*
* This hash is used automatically by Phaser Arcade Physics in order to perform non z-index based destructive sorting.
* However if you don't use Arcade Physics, or this isn't a physics enabled Group, then you can use the hash to perform your own
* sorting and filtering of Group children without touching their z-index (and therefore display draw order)
*
* @property {array} hash
*/
this.hash = [];
/**
* The property on which children are sorted.
* @property {string} _sortProperty
* @private
*/
this._sortProperty = 'z';
};
Phaser.Group.prototype = Object.create(PIXI.DisplayObjectContainer.prototype);
Phaser.Group.prototype.constructor = Phaser.Group;
/**
* A returnType value, as specified in {@link #iterate} eg.
* @constant
* @type {integer}
*/
Phaser.Group.RETURN_NONE = 0;
/**
* A returnType value, as specified in {@link #iterate} eg.
* @constant
* @type {integer}
*/
Phaser.Group.RETURN_TOTAL = 1;
/**
* A returnType value, as specified in {@link #iterate} eg.
* @constant
* @type {integer}
*/
Phaser.Group.RETURN_CHILD = 2;
/**
* A sort ordering value, as specified in {@link #sort} eg.
* @constant
* @type {integer}
*/
Phaser.Group.SORT_ASCENDING = -1;
/**
* A sort ordering value, as specified in {@link #sort} eg.
* @constant
* @type {integer}
*/
Phaser.Group.SORT_DESCENDING = 1;
/**
* Adds an existing object as the top child in this group.
*
* The child is automatically added to the top of the group and is displayed on top of every previous child.
*
* If Group.enableBody is set then a physics body will be created on the object, so long as one does not already exist.
*
* Use {@link #addAt} to control where a child is added. Use {@link #create} to create and add a new child.
*
* @method Phaser.Group#add
* @param {DisplayObject} child - The display object to add as a child.
* @param {boolean} [silent=false] - If true the child will not dispatch the `onAddedToGroup` event.
* @return {DisplayObject} The child that was added to the group.
*/
Phaser.Group.prototype.add = function (child, silent) {
if (silent === undefined) { silent = false; }
if (child.parent !== this)
{
this.addChild(child);
child.z = this.children.length;
if (this.enableBody && child.body === null)
{
this.game.physics.enable(child, this.physicsBodyType);
}
else if (child.body)
{
this.addToHash(child);
}
if (!silent && child.events)
{
child.events.onAddedToGroup$dispatch(child, this);
}
if (this.cursor === null)
{
this.cursor = child;
}
}
return child;
};
/**
* Adds a child of this Group into the hash array.
* This call will return false if the child is not a child of this Group, or is already in the hash.
*
* @method Phaser.Group#addToHash
* @param {DisplayObject} child - The display object to add to this Groups hash. Must be a member of this Group already and not present in the hash.
* @return {boolean} True if the child was successfully added to the hash, otherwise false.
*/
Phaser.Group.prototype.addToHash = function (child) {
if (child.parent === this)
{
var index = this.hash.indexOf(child);
if (index === -1)
{
this.hash.push(child);
return true;
}
}
return false;
};
/**
* Removes a child of this Group from the hash array.
* This call will return false if the child is not in the hash.
*
* @method Phaser.Group#removeFromHash
* @param {DisplayObject} child - The display object to remove from this Groups hash. Must be a member of this Group and in the hash.
* @return {boolean} True if the child was successfully removed from the hash, otherwise false.
*/
Phaser.Group.prototype.removeFromHash = function (child) {
if (child)
{
var index = this.hash.indexOf(child);
if (index !== -1)
{
this.hash.splice(index, 1);
return true;
}
}
return false;
};
/**
* Adds an array of existing Display Objects to this Group.
*
* The Display Objects are automatically added to the top of this Group, and will render on-top of everything already in this Group.
*
* As well as an array you can also pass another Group as the first argument. In this case all of the children from that
* Group will be removed from it and added into this Group.
*
* @method Phaser.Group#addMultiple
* @param {DisplayObject[]|Phaser.Group} children - An array of display objects or a Phaser.Group. If a Group is given then *all* children will be moved from it.
* @param {boolean} [silent=false] - If true the children will not dispatch the `onAddedToGroup` event.
* @return {DisplayObject[]|Phaser.Group} The array of children or Group of children that were added to this Group.
*/
Phaser.Group.prototype.addMultiple = function (children, silent) {
if (children instanceof Phaser.Group)
{
children.moveAll(this, silent);
}
else if (Array.isArray(children))
{
for (var i = 0; i < children.length; i++)
{
this.add(children[i], silent);
}
}
return children;
};
/**
* Adds an existing object to this group.
*
* The child is added to the group at the location specified by the index value, this allows you to control child ordering.
*
* @method Phaser.Group#addAt
* @param {DisplayObject} child - The display object to add as a child.
* @param {integer} [index=0] - The index within the group to insert the child to.
* @param {boolean} [silent=false] - If true the child will not dispatch the `onAddedToGroup` event.
* @return {DisplayObject} The child that was added to the group.
*/
Phaser.Group.prototype.addAt = function (child, index, silent) {
if (silent === undefined) { silent = false; }
if (child.parent !== this)
{
this.addChildAt(child, index);
this.updateZ();
if (this.enableBody && child.body === null)
{
this.game.physics.enable(child, this.physicsBodyType);
}
else if (child.body)
{
this.addToHash(child);
}
if (!silent && child.events)
{
child.events.onAddedToGroup$dispatch(child, this);
}
if (this.cursor === null)
{
this.cursor = child;
}
}
return child;
};
/**
* Returns the child found at the given index within this group.
*
* @method Phaser.Group#getAt
* @param {integer} index - The index to return the child from.
* @return {DisplayObject|integer} The child that was found at the given index, or -1 for an invalid index.
*/
Phaser.Group.prototype.getAt = function (index) {
if (index < 0 || index >= this.children.length)
{
return -1;
}
else
{
return this.getChildAt(index);
}
};
/**
* Creates a new Phaser.Sprite object and adds it to the top of this group.
*
* Use {@link #classType} to change the type of object created.
*
* @method Phaser.Group#create
* @param {number} x - The x coordinate to display the newly created Sprite at. The value is in relation to the group.x point.
* @param {number} y - The y coordinate to display the newly created Sprite at. The value is in relation to the group.y point.
* @param {string|Phaser.RenderTexture|Phaser.BitmapData|Phaser.Video|PIXI.Texture} [key] - This is the image or texture used by the Sprite during rendering. It can be a string which is a reference to the Cache Image entry, or an instance of a RenderTexture, BitmapData, Video or PIXI.Texture.
* @param {string|number} [frame] - If this Sprite is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index.
* @param {boolean} [exists=true] - The default exists state of the Sprite.
* @return {DisplayObject} The child that was created: will be a {@link Phaser.Sprite} unless {@link #classType} has been changed.
*/
Phaser.Group.prototype.create = function (x, y, key, frame, exists) {
if (exists === undefined) { exists = true; }
var child = new this.classType(this.game, x, y, key, frame);
child.exists = exists;
child.visible = exists;
child.alive = exists;
this.addChild(child);
child.z = this.children.length;
if (this.enableBody)
{
this.game.physics.enable(child, this.physicsBodyType, this.enableBodyDebug);
}
if (child.events)
{
child.events.onAddedToGroup$dispatch(child, this);
}
if (this.cursor === null)
{
this.cursor = child;
}
return child;
};
/**
* Creates multiple Phaser.Sprite objects and adds them to the top of this group.
*
* Useful if you need to quickly generate a pool of identical sprites, such as bullets.
*
* By default the sprites will be set to not exist and will be positioned at 0, 0 (relative to the group.x/y).
* Use {@link #classType} to change the type of object created.
*
* @method Phaser.Group#createMultiple
* @param {integer} quantity - The number of Sprites to create.
* @param {string} key - The Game.cache key of the image that this Sprite will use.
* @param {integer|string} [frame] - If the Sprite image contains multiple frames you can specify which one to use here.
* @param {boolean} [exists=false] - The default exists state of the Sprite.
*/
Phaser.Group.prototype.createMultiple = function (quantity, key, frame, exists) {
if (exists === undefined) { exists = false; }
for (var i = 0; i < quantity; i++)
{
this.create(0, 0, key, frame, exists);
}
};
/**
* Internal method that re-applies all of the children's Z values.
*
* This must be called whenever children ordering is altered so that their `z` indices are correctly updated.
*
* @method Phaser.Group#updateZ
* @protected
*/
Phaser.Group.prototype.updateZ = function () {
var i = this.children.length;
while (i--)
{
this.children[i].z = i;
}
};
/**
* Sets the group cursor to the first child in the group.
*
* If the optional index parameter is given it sets the cursor to the object at that index instead.
*
* @method Phaser.Group#resetCursor
* @param {integer} [index=0] - Set the cursor to point to a specific index.
* @return {any} The child the cursor now points to.
*/
Phaser.Group.prototype.resetCursor = function (index) {
if (index === undefined) { index = 0; }
if (index > this.children.length - 1)
{
index = 0;
}
if (this.cursor)
{
this.cursorIndex = index;
this.cursor = this.children[this.cursorIndex];
return this.cursor;
}
};
/**
* Advances the group cursor to the next (higher) object in the group.
*
* If the cursor is at the end of the group (top child) it is moved the start of the group (bottom child).
*
* @method Phaser.Group#next
* @return {any} The child the cursor now points to.
*/
Phaser.Group.prototype.next = function () {
if (this.cursor)
{
// Wrap the cursor?
if (this.cursorIndex >= this.children.length - 1)
{
this.cursorIndex = 0;
}
else
{
this.cursorIndex++;
}
this.cursor = this.children[this.cursorIndex];
return this.cursor;
}
};
/**
* Moves the group cursor to the previous (lower) child in the group.
*
* If the cursor is at the start of the group (bottom child) it is moved to the end (top child).
*
* @method Phaser.Group#previous
* @return {any} The child the cursor now points to.
*/
Phaser.Group.prototype.previous = function () {
if (this.cursor)
{
// Wrap the cursor?
if (this.cursorIndex === 0)
{
this.cursorIndex = this.children.length - 1;
}
else
{
this.cursorIndex--;
}
this.cursor = this.children[this.cursorIndex];
return this.cursor;
}
};
/**
* Swaps the position of two children in this group.
*
* Both children must be in this group, a child cannot be swapped with itself, and unparented children cannot be swapped.
*
* @method Phaser.Group#swap
* @param {any} child1 - The first child to swap.
* @param {any} child2 - The second child to swap.
*/
Phaser.Group.prototype.swap = function (child1, child2) {
this.swapChildren(child1, child2);
this.updateZ();
};
/**
* Brings the given child to the top of this group so it renders above all other children.
*
* @method Phaser.Group#bringToTop
* @param {any} child - The child to bring to the top of this group.
* @return {any} The child that was moved.
*/
Phaser.Group.prototype.bringToTop = function (child) {
if (child.parent === this && this.getIndex(child) < this.children.length)
{
this.remove(child, false, true);
this.add(child, true);
}
return child;
};
/**
* Sends the given child to the bottom of this group so it renders below all other children.
*
* @method Phaser.Group#sendToBack
* @param {any} child - The child to send to the bottom of this group.
* @return {any} The child that was moved.
*/
Phaser.Group.prototype.sendToBack = function (child) {
if (child.parent === this && this.getIndex(child) > 0)
{
this.remove(child, false, true);
this.addAt(child, 0, true);
}
return child;
};
/**
* Moves the given child up one place in this group unless it's already at the top.
*
* @method Phaser.Group#moveUp
* @param {any} child - The child to move up in the group.
* @return {any} The child that was moved.
*/
Phaser.Group.prototype.moveUp = function (child) {
if (child.parent === this && this.getIndex(child) < this.children.length - 1)
{
var a = this.getIndex(child);
var b = this.getAt(a + 1);
if (b)
{
this.swap(child, b);
}
}
return child;
};
/**
* Moves the given child down one place in this group unless it's already at the bottom.
*
* @method Phaser.Group#moveDown
* @param {any} child - The child to move down in the group.
* @return {any} The child that was moved.
*/
Phaser.Group.prototype.moveDown = function (child) {
if (child.parent === this && this.getIndex(child) > 0)
{
var a = this.getIndex(child);
var b = this.getAt(a - 1);
if (b)
{
this.swap(child, b);
}
}
return child;
};
/**
* Positions the child found at the given index within this group to the given x and y coordinates.
*
* @method Phaser.Group#xy
* @param {integer} index - The index of the child in the group to set the position of.
* @param {number} x - The new x position of the child.
* @param {number} y - The new y position of the child.
*/
Phaser.Group.prototype.xy = function (index, x, y) {
if (index < 0 || index > this.children.length)
{
return -1;
}
else
{
this.getChildAt(index).x = x;
this.getChildAt(index).y = y;
}
};
/**
* Reverses all children in this group.
*
* This operation applies only to immediate children and does not propagate to subgroups.
*
* @method Phaser.Group#reverse
*/
Phaser.Group.prototype.reverse = function () {
this.children.reverse();
this.updateZ();
};
/**
* Get the index position of the given child in this group, which should match the child's `z` property.
*
* @method Phaser.Group#getIndex
* @param {any} child - The child to get the index for.
* @return {integer} The index of the child or -1 if it's not a member of this group.
*/
Phaser.Group.prototype.getIndex = function (child) {
return this.children.indexOf(child);
};
/**
* Replaces a child of this group with the given newChild. The newChild cannot be a member of this group.
*
* @method Phaser.Group#replace
* @param {any} oldChild - The child in this group that will be replaced.
* @param {any} newChild - The child to be inserted into this group.
* @return {any} Returns the oldChild that was replaced within this group.
*/
Phaser.Group.prototype.replace = function (oldChild, newChild) {
var index = this.getIndex(oldChild);
if (index !== -1)
{
if (newChild.parent)
{
if (newChild.parent instanceof Phaser.Group)
{
newChild.parent.remove(newChild);
}
else
{
newChild.parent.removeChild(newChild);
}
}
this.remove(oldChild);
this.addAt(newChild, index);
return oldChild;
}
};
/**
* Checks if the child has the given property.
*
* Will scan up to 4 levels deep only.
*
* @method Phaser.Group#hasProperty
* @param {any} child - The child to check for the existance of the property on.
* @param {string[]} key - An array of strings that make up the property.
* @return {boolean} True if the child has the property, otherwise false.
*/
Phaser.Group.prototype.hasProperty = function (child, key) {
var len = key.length;
if (len === 1 && key[0] in child)
{
return true;
}
else if (len === 2 && key[0] in child && key[1] in child[key[0]])
{
return true;
}
else if (len === 3 && key[0] in child && key[1] in child[key[0]] && key[2] in child[key[0]][key[1]])
{
return true;
}
else if (len === 4 && key[0] in child && key[1] in child[key[0]] && key[2] in child[key[0]][key[1]] && key[3] in child[key[0]][key[1]][key[2]])
{
return true;
}
return false;
};
/**
* Sets a property to the given value on the child. The operation parameter controls how the value is set.
*
* The operations are:
* - 0: set the existing value to the given value; if force is `true` a new property will be created if needed
* - 1: will add the given value to the value already present.
* - 2: will subtract the given value from the value already present.
* - 3: will multiply the value already present by the given value.
* - 4: will divide the value already present by the given value.
*
* @method Phaser.Group#setProperty
* @param {any} child - The child to set the property value on.
* @param {array} key - An array of strings that make up the property that will be set.
* @param {any} value - The value that will be set.
* @param {integer} [operation=0] - Controls how the value is assigned. A value of 0 replaces the value with the new one. A value of 1 adds it, 2 subtracts it, 3 multiplies it and 4 divides it.
* @param {boolean} [force=false] - If `force` is true then the property will be set on the child regardless if it already exists or not. If false and the property doesn't exist, nothing will be set.
* @return {boolean} True if the property was set, false if not.
*/
Phaser.Group.prototype.setProperty = function (child, key, value, operation, force) {
if (force === undefined) { force = false; }
operation = operation || 0;
// As ugly as this approach looks, and although it's limited to a depth of only 4, it's much faster than a for loop or object iteration.
// 0 = Equals
// 1 = Add
// 2 = Subtract
// 3 = Multiply
// 4 = Divide
// We can't force a property in and the child doesn't have it, so abort.
// Equally we can't add, subtract, multiply or divide a property value if it doesn't exist, so abort in those cases too.
if (!this.hasProperty(child, key) && (!force || operation > 0))
{
return false;
}
var len = key.length;
if (len === 1)
{
if (operation === 0) { child[key[0]] = value; }
else if (operation == 1) { child[key[0]] += value; }
else if (operation == 2) { child[key[0]] -= value; }
else if (operation == 3) { child[key[0]] *= value; }
else if (operation == 4) { child[key[0]] /= value; }
}
else if (len === 2)
{
if (operation === 0) { child[key[0]][key[1]] = value; }
else if (operation == 1) { child[key[0]][key[1]] += value; }
else if (operation == 2) { child[key[0]][key[1]] -= value; }
else if (operation == 3) { child[key[0]][key[1]] *= value; }
else if (operation == 4) { child[key[0]][key[1]] /= value; }
}
else if (len === 3)
{
if (operation === 0) { child[key[0]][key[1]][key[2]] = value; }
else if (operation == 1) { child[key[0]][key[1]][key[2]] += value; }
else if (operation == 2) { child[key[0]][key[1]][key[2]] -= value; }
else if (operation == 3) { child[key[0]][key[1]][key[2]] *= value; }
else if (operation == 4) { child[key[0]][key[1]][key[2]] /= value; }
}
else if (len === 4)
{
if (operation === 0) { child[key[0]][key[1]][key[2]][key[3]] = value; }
else if (operation == 1) { child[key[0]][key[1]][key[2]][key[3]] += value; }
else if (operation == 2) { child[key[0]][key[1]][key[2]][key[3]] -= value; }
else if (operation == 3) { child[key[0]][key[1]][key[2]][key[3]] *= value; }
else if (operation == 4) { child[key[0]][key[1]][key[2]][key[3]] /= value; }
}
return true;
};
/**
* Checks a property for the given value on the child.
*
* @method Phaser.Group#checkProperty
* @param {any} child - The child to check the property value on.
* @param {array} key - An array of strings that make up the property that will be set.
* @param {any} value - The value that will be checked.
* @param {boolean} [force=false] - If `force` is true then the property will be checked on the child regardless if it already exists or not. If true and the property doesn't exist, false will be returned.
* @return {boolean} True if the property was was equal to value, false if not.
*/
Phaser.Group.prototype.checkProperty = function (child, key, value, force) {
if (force === undefined) { force = false; }
// We can't force a property in and the child doesn't have it, so abort.
if (!Phaser.Utils.getProperty(child, key) && force)
{
return false;
}
if (Phaser.Utils.getProperty(child, key) !== value)
{
return false;
}
return true;
};
/**
* Quickly set a property on a single child of this group to a new value.
*
* The operation parameter controls how the new value is assigned to the property, from simple replacement to addition and multiplication.
*
* @method Phaser.Group#set
* @param {Phaser.Sprite} child - The child to set the property on.
* @param {string} key - The property, as a string, to be set. For example: 'body.velocity.x'
* @param {any} value - The value that will be set.
* @param {boolean} [checkAlive=false] - If set then the child will only be updated if alive=true.
* @param {boolean} [checkVisible=false] - If set then the child will only be updated if visible=true.
* @param {integer} [operation=0] - Controls how the value is assigned. A value of 0 replaces the value with the new one. A value of 1 adds it, 2 subtracts it, 3 multiplies it and 4 divides it.
* @param {boolean} [force=false] - If `force` is true then the property will be set on the child regardless if it already exists or not. If false and the property doesn't exist, nothing will be set.
* @return {boolean} True if the property was set, false if not.
*/
Phaser.Group.prototype.set = function (child, key, value, checkAlive, checkVisible, operation, force) {
if (force === undefined) { force = false; }
key = key.split('.');
if (checkAlive === undefined) { checkAlive = false; }
if (checkVisible === undefined) { checkVisible = false; }
if ((checkAlive === false || (checkAlive && child.alive)) && (checkVisible === false || (checkVisible && child.visible)))
{
return this.setProperty(child, key, value, operation, force);
}
};
/**