@@ -21,6 +21,46 @@ var StableSort = require('../../utils/array/StableSort');
2121 * @classdesc
2222 * A Layer Game Object.
2323 *
24+ * A Layer is a special type of Game Object that acts as a Display List. You can add any type of Game Object
25+ * to a Layer, just as you would to a Scene. Layers can be used to visually group together 'layers' of Game
26+ * Objects:
27+ *
28+ * ```javascript
29+ * const spaceman = this.add.sprite(150, 300, 'spaceman');
30+ * const bunny = this.add.sprite(400, 300, 'bunny');
31+ * const elephant = this.add.sprite(650, 300, 'elephant');
32+ *
33+ * const layer = this.add.layer();
34+ *
35+ * layer.add([ spaceman, bunny, elephant ]);
36+ * ```
37+ *
38+ * The 3 sprites in the example above will now be managed by the Layer they were added to. Therefore,
39+ * if you then set `layer.setVisible(false)` they would all vanish from the display.
40+ *
41+ * You can also control the depth of the Game Objects within the Layer. For example, calling the
42+ * `setDepth` method of a child of a Layer will allow you to adjust the depth of that child _within the
43+ * Layer itself_, rather than the whole Scene. The Layer, too, can have its depth set as well.
44+ *
45+ * The Layer class also offers many different methods for manipulating the list, such as the
46+ * methods `moveUp`, `moveDown`, `sendToBack`, `bringToTop` and so on. These allow you to change the
47+ * display list position of the Layers children, causing it to adjust the order in which they are
48+ * rendered. Using `setDepth` on a child allows you to override this.
49+ *
50+ * Layers can have Post FX Pipelines set, which allows you to easily enable a post pipeline across
51+ * a whole range of children, which, depending on the effect, can often be far more efficient that doing so
52+ * on a per-child basis.
53+ *
54+ * Layers have no position or size within the Scene. This means you cannot enable a Layer for
55+ * physics or input, or change the position, rotation or scale of a Layer. They also have no scroll
56+ * factor, texture, tint, origin, crop or bounds.
57+ *
58+ * If you need those kind of features then you should use a Container instead. Containers can be added
59+ * to Layers, but Layers cannot be added to Containers.
60+ *
61+ * However, you can set the Alpha, Blend Mode, Depth, Mask and Visible state of a Layer. These settings
62+ * will impact all children being rendered by the Layer.
63+ *
2464 * @class Layer
2565 * @extends Phaser.Structs.List.<Phaser.GameObjects.GameObject>
2666 * @memberof Phaser.GameObjects
@@ -73,6 +113,20 @@ var Layer = new Class({
73113 */
74114 this . scene = scene ;
75115
116+ /**
117+ * Holds a reference to the Display List that contains this Game Object.
118+ *
119+ * This is set automatically when this Game Object is added to a Scene or Layer.
120+ *
121+ * You should treat this property as being read-only.
122+ *
123+ * @name Phaser.GameObjects.Layer#displayList
124+ * @type {(Phaser.GameObjects.DisplayList|Phaser.GameObjects.Layer) }
125+ * @default null
126+ * @since 3.50.0
127+ */
128+ this . displayList = null ;
129+
76130 /**
77131 * A textual representation of this Game Object, i.e. `sprite`.
78132 * Used internally by Phaser but is available for your own custom classes to populate.
@@ -544,7 +598,12 @@ var Layer = new Class({
544598 {
545599 gameObject . emit ( GameObjectEvents . ADDED_TO_SCENE , gameObject , this . scene ) ;
546600
547- gameObject . depthList = this ;
601+ if ( gameObject . displayList )
602+ {
603+ gameObject . displayList . remove ( gameObject ) ;
604+ }
605+
606+ gameObject . displayList = this ;
548607
549608 this . queueDepthSort ( ) ;
550609
@@ -566,7 +625,7 @@ var Layer = new Class({
566625 {
567626 gameObject . emit ( GameObjectEvents . REMOVED_FROM_SCENE , gameObject , this . scene ) ;
568627
569- gameObject . depthList = null ;
628+ gameObject . displayList = null ;
570629
571630 this . queueDepthSort ( ) ;
572631
@@ -664,11 +723,10 @@ var Layer = new Class({
664723
665724 this . emit ( GameObjectEvents . DESTROY , this ) ;
666725
667- var sys = this . systems ;
668-
669- if ( ! fromScene )
726+ if ( ! fromScene && this . displayList )
670727 {
671- sys . displayList . remove ( this ) ;
728+ this . displayList . remove ( this ) ;
729+ this . displayList . queueDepthSort ( ) ;
672730 }
673731
674732 if ( this . data )
@@ -678,16 +736,11 @@ var Layer = new Class({
678736 this . data = undefined ;
679737 }
680738
681- // Tell the Scene to re-sort the children
682- if ( ! fromScene )
683- {
684- sys . queueDepthSort ( ) ;
685- }
686-
687739 this . active = false ;
688740 this . visible = false ;
689741
690742 this . scene = undefined ;
743+ this . displayList = undefined ;
691744 this . systems = undefined ;
692745 this . events = undefined ;
693746 }
0 commit comments