Skip to content

Commit 0e14b43

Browse files
committed
Added maxSize, jsdocs and ts def fixes
1 parent 9bc6237 commit 0e14b43

1 file changed

Lines changed: 71 additions & 68 deletions

File tree

src/gameobjects/container/Container.js

Lines changed: 71 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,36 @@ var Vector2 = require('../../math/Vector2');
1818
* @classdesc
1919
* A Container Game Object.
2020
*
21+
* A Container, as the name implies, can 'contain' other types of Game Object.
22+
* When a Game Object is added to a Container, the Container becomes responsible for the rendering of it.
23+
* By default it will be removed from the Display List and instead added to the Containers own internal list.
24+
*
25+
* The position of the Game Object automatically becomes relative to the position of the Container.
26+
*
27+
* When the Container is rendered, all of its children are rendered as well, in the order in which they exist
28+
* within the Container. Container children can be repositioned using methods such as `MoveUp`, `MoveDown` and `SendToBack`.
29+
*
30+
* If you modify a transform property of the Container, such as `Container.x` or `Container.rotation` then it will
31+
* automatically influence all children as well.
32+
*
33+
* Containers can include other Containers for deeply nested transforms.
34+
*
35+
* Containers can have masks set on them and can be used as a mask too. However, Container children cannot be masked.
36+
* The masks do not 'stack up'. Only a Container on the root of the display list will use its mask.
37+
*
38+
* Containers can be enabled for input. Because they do not have a texture you need to provide a shape for them
39+
* to use as their hit area. Container children can also be enabled for input, independent of the Container.
40+
*
41+
* Containers can be given a physics body for either Arcade Physics, Impact Physics or Matter Physics. However,
42+
* if Container children are enabled for physics you may get unexpected results,such as offset bodies,
43+
* if the Container itself, or any of its ancestors, is positioned anywhere other than at 0x0.
44+
*
45+
* It's important to understand the impact of using Containers. They add additional processing overhead into
46+
* every one of their children. The deeper you nest them, the more the cost escalates. This is especially true
47+
* for input events. You also loose the ability to set the display depth of Container children in the same
48+
* flexible manner as those not within them. In short, don't use them for the sake of it. You pay a small cost
49+
* every time you create one, try to structure your game around avoiding that where possible.
50+
*
2151
* @class Container
2252
* @extends Phaser.GameObjects.GameObject
2353
* @memberOf Phaser.GameObjects
@@ -87,6 +117,18 @@ var Container = new Class({
87117
*/
88118
this.exclusive = true;
89119

120+
/**
121+
* Containers can have an optional maximum size. If set to anything above 0 it
122+
* will constrict the addition of new Game Objects into the Container, capping off
123+
* the maximum limit the Container can grow in size to.
124+
*
125+
* @name Phaser.GameObjects.Container#maxSize
126+
* @type {integer}
127+
* @default -1
128+
* @since 3.4.0
129+
*/
130+
this.maxSize = -1;
131+
90132
/**
91133
* The cursor position.
92134
*
@@ -336,71 +378,66 @@ var Container = new Class({
336378
* @method Phaser.GameObjects.Container#add
337379
* @since 3.4.0
338380
*
339-
* @genericUse {T} - [child,$return]
340-
*
341-
* @param {*|Array.<*>} child - The item, or array of items, to add to the array. Each item must be unique within the array.
381+
* @param {Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]} child - The Game Object, or array of Game Objects, to add to the Container.
342382
*
343383
* @return {Phaser.GameObjects.Container} This Container instance.
344384
*/
345385
add: function (child)
346386
{
347-
ArrayUtils.Add(this.list, child, 0, this.addHandler, this);
387+
ArrayUtils.Add(this.list, child, this.maxSize, this.addHandler, this);
348388

349389
return this;
350390
},
351391

352392
/**
353-
* [description]
393+
* Adds the given Game Object, or array of Game Objects, to this Container at the specified position.
394+
*
395+
* Existing Game Objects in the Container are shifted up.
396+
*
397+
* Each Game Object must be unique within the Container.
354398
*
355399
* @method Phaser.GameObjects.Container#addAt
356400
* @since 3.4.0
357401
*
358-
* @genericUse {T} - [child,$return]
359-
*
360-
* @param {*} child - [description]
361-
* @param {integer} [index=0] - [description]
402+
* @param {Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]} child - The Game Object, or array of Game Objects, to add to the Container.
403+
* @param {integer} [index=0] - The position to insert the Game Object/s at.
362404
*
363405
* @return {Phaser.GameObjects.Container} This Container instance.
364406
*/
365407
addAt: function (child, index)
366408
{
367-
ArrayUtils.AddAt(this.list, child, index, 0, this.addHandler, this);
409+
ArrayUtils.AddAt(this.list, child, index, this.maxSize, this.addHandler, this);
368410

369411
return this;
370412
},
371413

372414
/**
373-
* [description]
415+
* Returns the Game Object at the given position in this Container.
374416
*
375417
* @method Phaser.GameObjects.Container#getAt
376418
* @since 3.4.0
377419
*
378-
* @genericUse {T} - [$return]
379-
*
380-
* @param {integer} index - [description]
420+
* @param {integer} index - The position to get the Game Object from.
381421
*
382-
* @return {Phaser.GameObjects.GameObject|null} The Game Object at the specified index, or `null` if none found.
422+
* @return {?Phaser.GameObjects.GameObject} The Game Object at the specified index, or `null` if none found.
383423
*/
384424
getAt: function (index)
385425
{
386426
return this.list[index];
387427
},
388428

389429
/**
390-
* [description]
430+
* Returns the index of the given Game Object in this Container.
391431
*
392432
* @method Phaser.GameObjects.Container#getIndex
393433
* @since 3.4.0
394434
*
395-
* @genericUse {T} - [child]
396-
*
397435
* @param {Phaser.GameObjects.GameObject} child - The Game Object to search for in this Container.
398436
*
399437
* @return {integer} The index of the Game Object in this Container, or -1 if not found.
400438
*/
401439
getIndex: function (child)
402440
{
403-
// Return -1 if given child isn't a child of this display list
404441
return this.list.indexOf(child);
405442
},
406443

@@ -411,8 +448,6 @@ var Container = new Class({
411448
* @method Phaser.GameObjects.Container#sort
412449
* @since 3.4.0
413450
*
414-
* @genericUse {T[]} - [children,$return]
415-
*
416451
* @param {string} property - The property to lexically sort by.
417452
*
418453
* @return {Phaser.GameObjects.Container} This Container instance.
@@ -436,8 +471,6 @@ var Container = new Class({
436471
* @private
437472
* @since 3.4.0
438473
*
439-
* @genericUse {T} - [childA,childB]
440-
*
441474
* @param {Phaser.GameObjects.GameObject} childA - The first child to sort.
442475
* @param {Phaser.GameObjects.GameObject} childB - The second child to sort.
443476
*
@@ -455,8 +488,6 @@ var Container = new Class({
455488
* @method Phaser.GameObjects.Container#getByName
456489
* @since 3.4.0
457490
*
458-
* @genericUse {T | null} - [$return]
459-
*
460491
* @param {string} name - The name to search for.
461492
*
462493
* @return {?Phaser.GameObjects.GameObject} The first child with a matching name, or `null` if none were found.
@@ -472,8 +503,6 @@ var Container = new Class({
472503
* @method Phaser.GameObjects.Container#getRandom
473504
* @since 3.4.0
474505
*
475-
* @genericUse {T | null} - [$return]
476-
*
477506
* @param {integer} [startIndex=0] - An optional start index.
478507
* @param {integer} [length] - An optional length, the total number of elements (from the startIndex) to choose from.
479508
*
@@ -528,7 +557,7 @@ var Container = new Class({
528557
* @since 3.4.0
529558
*
530559
* @param {string} [property] - The property to test on each Game Object in the Container.
531-
* @param {*} [value] - If property is set then the `property` must strictly equal this value to be included in the results.
560+
* @param {any} [value] - If property is set then the `property` must strictly equal this value to be included in the results.
532561
* @param {integer} [startIndex=0] - An optional start index to search from.
533562
* @param {integer} [endIndex=Container.length] - An optional end index to search up to (but not included)
534563
*
@@ -551,7 +580,7 @@ var Container = new Class({
551580
* @since 3.4.0
552581
*
553582
* @param {string} property - [description]
554-
* @param {*} value - [description]
583+
* @param {any} value - [description]
555584
* @param {integer} [startIndex=0] - An optional start index to search from.
556585
* @param {integer} [endIndex=Container.length] - An optional end index to search up to (but not included)
557586
*
@@ -569,8 +598,6 @@ var Container = new Class({
569598
* @method Phaser.GameObjects.Container#swap
570599
* @since 3.4.0
571600
*
572-
* @genericUse {T} - [child1,child2]
573-
*
574601
* @param {Phaser.GameObjects.GameObject} child1 - The first Game Object to swap.
575602
* @param {Phaser.GameObjects.GameObject} child2 - The second Game Object to swap.
576603
*
@@ -594,8 +621,6 @@ var Container = new Class({
594621
* @method Phaser.GameObjects.Container#moveTo
595622
* @since 3.4.0
596623
*
597-
* @genericUse {T} - [child,$return]
598-
*
599624
* @param {Phaser.GameObjects.GameObject} child - The Game Object to move.
600625
* @param {integer} index - The new position of the Game Object in this Container.
601626
*
@@ -618,8 +643,6 @@ var Container = new Class({
618643
* @method Phaser.GameObjects.Container#remove
619644
* @since 3.4.0
620645
*
621-
* @genericUse {T} - [child,$return]
622-
*
623646
* @param {Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]} child - The Game Object, or array of Game Objects, to be removed from the Container.
624647
* @param {boolean} [destroyChild=false] - Optionally call `destroy` on each child successfully removed from this Container.
625648
*
@@ -653,8 +676,6 @@ var Container = new Class({
653676
* @method Phaser.GameObjects.Container#removeAt
654677
* @since 3.4.0
655678
*
656-
* @genericUse {T} - [$return]
657-
*
658679
* @param {integer} index - The index of the Game Object to be removed.
659680
* @param {boolean} [destroyChild=false] - Optionally call `destroy` on the Game Object if successfully removed from this Container.
660681
*
@@ -680,8 +701,6 @@ var Container = new Class({
680701
* @method Phaser.GameObjects.Container#removeBetween
681702
* @since 3.4.0
682703
*
683-
* @genericUse {T[]} - [$return]
684-
*
685704
* @param {integer} [startIndex=0] - An optional start index to search from.
686705
* @param {integer} [endIndex=Container.length] - An optional end index to search up to (but not included)
687706
* @param {boolean} [destroyChild=false] - Optionally call `destroy` on each Game Object successfully removed from this Container.
@@ -711,8 +730,6 @@ var Container = new Class({
711730
* @method Phaser.GameObjects.Container#removeAll
712731
* @since 3.4.0
713732
*
714-
* @genericUse {Phaser.GameObjects.Container.<T>} - [$return]
715-
*
716733
* @param {boolean} [destroyChild=false] - Optionally call `destroy` on each Game Object successfully removed from this Container.
717734
*
718735
* @return {Phaser.GameObjects.Container} This Container instance.
@@ -739,8 +756,6 @@ var Container = new Class({
739756
* @method Phaser.GameObjects.Container#bringToTop
740757
* @since 3.4.0
741758
*
742-
* @genericUse {T} - [child,$return]
743-
*
744759
* @param {Phaser.GameObjects.GameObject} child - The Game Object to bring to the top of the Container.
745760
*
746761
* @return {Phaser.GameObjects.Container} This Container instance.
@@ -759,8 +774,6 @@ var Container = new Class({
759774
* @method Phaser.GameObjects.Container#sendToBack
760775
* @since 3.4.0
761776
*
762-
* @genericUse {T} - [child,$return]
763-
*
764777
* @param {Phaser.GameObjects.GameObject} child - The Game Object to send to the bottom of the Container.
765778
*
766779
* @return {Phaser.GameObjects.Container} This Container instance.
@@ -778,8 +791,6 @@ var Container = new Class({
778791
* @method Phaser.GameObjects.Container#moveUp
779792
* @since 3.4.0
780793
*
781-
* @genericUse {T} - [child,$return]
782-
*
783794
* @param {Phaser.GameObjects.GameObject} child - The Game Object to be moved in the Container.
784795
*
785796
* @return {Phaser.GameObjects.Container} This Container instance.
@@ -797,8 +808,6 @@ var Container = new Class({
797808
* @method Phaser.GameObjects.Container#moveDown
798809
* @since 3.4.0
799810
*
800-
* @genericUse {T} - [child,$return]
801-
*
802811
* @param {Phaser.GameObjects.GameObject} child - The Game Object to be moved in the Container.
803812
*
804813
* @return {Phaser.GameObjects.Container} This Container instance.
@@ -816,8 +825,6 @@ var Container = new Class({
816825
* @method Phaser.GameObjects.Container#reverse
817826
* @since 3.4.0
818827
*
819-
* @genericUse {Phaser.GameObjects.Container.<T>} - [$return]
820-
*
821828
* @return {Phaser.GameObjects.Container} This Container instance.
822829
*/
823830
reverse: function ()
@@ -833,8 +840,6 @@ var Container = new Class({
833840
* @method Phaser.GameObjects.Container#shuffle
834841
* @since 3.4.0
835842
*
836-
* @genericUse {Phaser.GameObjects.Container.<T>} - [$return]
837-
*
838843
* @return {Phaser.GameObjects.Container} This Container instance.
839844
*/
840845
shuffle: function ()
@@ -851,8 +856,6 @@ var Container = new Class({
851856
* @method Phaser.GameObjects.Container#replace
852857
* @since 3.4.0
853858
*
854-
* @genericUse {T} - [oldChild,newChild,$return]
855-
*
856859
* @param {Phaser.GameObjects.GameObject} oldChild - The Game Object in this Container that will be replaced.
857860
* @param {Phaser.GameObjects.GameObject} newChild - The Game Object to be added to this Container.
858861
* @param {boolean} [destroyChild=false] - Optionally call `destroy` on the Game Object if successfully removed from this Container.
@@ -885,8 +888,6 @@ var Container = new Class({
885888
* @method Phaser.GameObjects.Container#exists
886889
* @since 3.4.0
887890
*
888-
* @genericUse {T} - [child]
889-
*
890891
* @param {Phaser.GameObjects.GameObject} child - The Game Object to check for within this Container.
891892
*
892893
* @return {boolean} True if the Game Object is an immediate child of this Container, otherwise false.
@@ -906,10 +907,8 @@ var Container = new Class({
906907
* @method Phaser.GameObjects.Container#setAll
907908
* @since 3.4.0
908909
*
909-
* @genericUse {T} - [value]
910-
*
911910
* @param {string} property - The property that must exist on the Game Object.
912-
* @param {*} value - The value to get the property to.
911+
* @param {any} value - The value to get the property to.
913912
* @param {integer} [startIndex=0] - An optional start index to search from.
914913
* @param {integer} [endIndex=Container.length] - An optional end index to search up to (but not included)
915914
*
@@ -922,6 +921,14 @@ var Container = new Class({
922921
return this;
923922
},
924923

924+
/**
925+
* @callback EachContainerCallback
926+
* @generic I - [item]
927+
*
928+
* @param {*} item - [description]
929+
* @param {...*} [args] - Additional arguments that will be passed to the callback, after the child.
930+
*/
931+
925932
/**
926933
* Passes all Game Objects in this Container to the given callback.
927934
*
@@ -934,10 +941,8 @@ var Container = new Class({
934941
* @method Phaser.GameObjects.Container#each
935942
* @since 3.4.0
936943
*
937-
* @genericUse {EachListCallback.<T>} - [callback]
938-
*
939-
* @param {EachListCallback} callback - The function to call.
940-
* @param {*} [context] - Value to use as `this` when executing callback.
944+
* @param {function} callback - The function to call.
945+
* @param {object} [context] - Value to use as `this` when executing callback.
941946
* @param {...*} [args] - Additional arguments that will be passed to the callback, after the child.
942947
*
943948
* @return {Phaser.GameObjects.Container} This Container instance.
@@ -973,10 +978,8 @@ var Container = new Class({
973978
* @method Phaser.GameObjects.Container#iterate
974979
* @since 3.4.0
975980
*
976-
* @genericUse {EachListCallback.<T>} - [callback]
977-
*
978-
* @param {EachListCallback} callback - The function to call.
979-
* @param {*} [context] - Value to use as `this` when executing callback.
981+
* @param {function} callback - The function to call.
982+
* @param {object} [context] - Value to use as `this` when executing callback.
980983
* @param {...*} [args] - Additional arguments that will be passed to the callback, after the child.
981984
*
982985
* @return {Phaser.GameObjects.Container} This Container instance.

0 commit comments

Comments
 (0)