Skip to content

Commit e96cbde

Browse files
committed
Added new displayList property and Layer documentation
1 parent aa06070 commit e96cbde

4 files changed

Lines changed: 98 additions & 35 deletions

File tree

src/gameobjects/DisplayList.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,14 @@ var DisplayList = new Class({
110110
{
111111
gameObject.emit(GameObjectEvents.ADDED_TO_SCENE, gameObject, this.scene);
112112

113-
gameObject.depthList = this;
113+
if (gameObject.displayList)
114+
{
115+
gameObject.displayList.remove(gameObject);
116+
}
117+
118+
gameObject.displayList = this;
119+
120+
this.queueDepthSort();
114121

115122
this.events.emit(SceneEvents.ADDED_TO_SCENE, gameObject, this.scene);
116123
},
@@ -130,7 +137,9 @@ var DisplayList = new Class({
130137
{
131138
gameObject.emit(GameObjectEvents.REMOVED_FROM_SCENE, gameObject, this.scene);
132139

133-
gameObject.depthList = null;
140+
gameObject.displayList = null;
141+
142+
this.queueDepthSort();
134143

135144
this.events.emit(SceneEvents.REMOVED_FROM_SCENE, gameObject, this.scene);
136145
},

src/gameobjects/GameObject.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ var GameObject = new Class({
4949
*/
5050
this.scene = scene;
5151

52+
/**
53+
* Holds a reference to the Display List that contains this Game Object.
54+
*
55+
* This is set automatically when this Game Object is added to a Scene or Layer.
56+
*
57+
* You should treat this property as being read-only.
58+
*
59+
* @name Phaser.GameObjects.GameObject#displayList
60+
* @type {(Phaser.GameObjects.DisplayList|Phaser.GameObjects.Layer)}
61+
* @default null
62+
* @since 3.50.0
63+
*/
64+
this.displayList = null;
65+
5266
/**
5367
* A textual representation of this Game Object, i.e. `sprite`.
5468
* Used internally by Phaser but is available for your own custom classes to populate.
@@ -679,16 +693,15 @@ var GameObject = new Class({
679693

680694
this.emit(Events.DESTROY, this);
681695

682-
var sys = this.scene.sys;
683-
684696
if (!fromScene)
685697
{
686-
sys.displayList.remove(this);
698+
this.displayList.remove(this);
687699
}
688700

689701
if (this.input)
690702
{
691-
sys.input.clear(this);
703+
this.scene.sys.input.clear(this);
704+
692705
this.input = undefined;
693706
}
694707

@@ -702,6 +715,7 @@ var GameObject = new Class({
702715
if (this.body)
703716
{
704717
this.body.destroy();
718+
705719
this.body = undefined;
706720
}
707721

@@ -710,14 +724,14 @@ var GameObject = new Class({
710724
// Tell the Scene to re-sort the children
711725
if (!fromScene)
712726
{
713-
sys.queueDepthSort();
727+
this.displayList.queueDepthSort();
714728
}
715729

716730
this.active = false;
717731
this.visible = false;
718732

719733
this.scene = undefined;
720-
734+
this.displayList = undefined;
721735
this.parentContainer = undefined;
722736

723737
this.removeAllListeners();

src/gameobjects/components/Depth.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,6 @@ var Depth = {
2525
*/
2626
_depth: 0,
2727

28-
/**
29-
* Holds a reference to the Display List responsible for depth sorting
30-
* this Game Object. This is set automatically when this Game Object is
31-
* added to the Display List in a Scene, or to a Layer. You should treat
32-
* this property as being read-only.
33-
*
34-
* @name Phaser.GameObjects.Components.Depth#depthList
35-
* @type {(Phaser.GameObjects.DisplayList|Phaser.GameObjects.Layer)}
36-
* @default null
37-
* @since 3.50.0
38-
*/
39-
depthList: null,
40-
4128
/**
4229
* The depth of this Game Object within the Scene.
4330
*
@@ -62,9 +49,9 @@ var Depth = {
6249

6350
set: function (value)
6451
{
65-
if (this.depthList)
52+
if (this.displayList)
6653
{
67-
this.depthList.queueDepthSort();
54+
this.displayList.queueDepthSort();
6855
}
6956

7057
this._depth = value;

src/gameobjects/layer/Layer.js

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)