Skip to content

Commit 5379717

Browse files
committed
Fixed Group.sort. Added z-depth property to all core game objects. Fixed P2 const overwrite.
1 parent b4cb281 commit 5379717

6 files changed

Lines changed: 139 additions & 81 deletions

File tree

examples/groups/depth sort.js

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,52 +22,28 @@ var locs = [];
2222
function create() {
2323

2424
// Create our tilemap to walk around
25-
// map = game.add.tilemap('desert');
26-
// map.addTilesetImage('ground_1x1');
27-
// layer = map.createLayer('Tile Layer 1');
25+
map = game.add.tilemap('desert');
26+
map.addTilesetImage('ground_1x1');
27+
layer = map.createLayer('Tile Layer 1');
2828

2929
// This group will hold the main player + all the tree sprites to depth sort against
3030
group = game.add.group();
3131

3232
// Create some trees, each in a unique location (otherwise we'll get sort flicker)
33-
for (var i = 0; i < 100; i++)
33+
for (var i = 0; i < 200; i++)
3434
{
3535
createUniqueLocation();
3636
}
3737

3838
sprite = group.create(300, 32, 'phaser');
3939

40-
// The player
41-
// group.create(128, 0, 'trees', game.rnd.integerInRange(0, 7));
42-
// group.create(64, 0, 'trees', game.rnd.integerInRange(0, 7));
43-
// group.create(256, 0, 'trees', game.rnd.integerInRange(0, 7));
44-
45-
// group.create(128, 32, 'trees', game.rnd.integerInRange(0, 7));
46-
// group.create(64, 32, 'trees', game.rnd.integerInRange(0, 7));
47-
// group.create(256, 32, 'trees', game.rnd.integerInRange(0, 7));
48-
49-
50-
// group.sort();
51-
52-
// dump();
53-
54-
// group.children[0].alpha = 0.2;
55-
// group.children[100].alpha = 0.2;
40+
group.sort();
5641

5742
// Move it
5843
cursors = game.input.keyboard.createCursorKeys();
5944

6045
}
6146

62-
function dump() {
63-
64-
for (var i = 0; i < group.children.length; i++)
65-
{
66-
console.log('Tree',i,'at',group.children[i].y);
67-
}
68-
69-
}
70-
7147
function createUniqueLocation() {
7248

7349
var x = game.math.snapTo(game.world.randomX, 32) / 32;
@@ -110,17 +86,14 @@ function update() {
11086
sprite.y += 2;
11187
}
11288

113-
// if (sprite.y !== oldY)
114-
// {
115-
group.sort();
116-
// oldY = sprite.y;
117-
// }
89+
group.sort('y', Phaser.Group.SORT_ASCENDING);
11890

11991
}
12092

12193
function render() {
12294

123-
game.debug.text(sprite.y, 32, 32);
124-
game.debug.text(group.getIndex(sprite), 32, 64);
95+
// game.debug.text(sprite.y, 32, 32);
96+
// game.debug.text(group.getIndex(sprite), 32, 64);
97+
// game.debug.text(sprite.z, 32, 64);
12598

12699
}

src/core/Group.js

Lines changed: 109 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ Phaser.Group = function (game, parent, name, addToStage) {
4747
}
4848
}
4949

50+
/**
51+
* @property {number} z - The z-depth value of this object within its Group (remember the World is a Group as well). No two objects in a Group can have the same z value.
52+
*/
53+
this.z = 0;
54+
5055
/**
5156
* @property {number} type - Internal Phaser Type value.
5257
* @protected
@@ -104,7 +109,7 @@ Phaser.Group = function (game, parent, name, addToStage) {
104109
* @property {string} _sortProperty - The property on which children are sorted.
105110
* @private
106111
*/
107-
this._sortProperty = 'y';
112+
this._sortProperty = 'z';
108113

109114
/**
110115
* A small internal cache:
@@ -175,15 +180,17 @@ Phaser.Group.prototype.add = function (child) {
175180
{
176181
this.addChild(child);
177182

183+
child.z = this.children.length;
184+
178185
if (child.events)
179186
{
180187
child.events.onAddedToGroup.dispatch(child, this);
181188
}
182-
}
183189

184-
if (this.cursor === null)
185-
{
186-
this.cursor = child;
190+
if (this.cursor === null)
191+
{
192+
this.cursor = child;
193+
}
187194
}
188195

189196
return child;
@@ -205,15 +212,17 @@ Phaser.Group.prototype.addAt = function (child, index) {
205212
{
206213
this.addChildAt(child, index);
207214

215+
this.updateZ();
216+
208217
if (child.events)
209218
{
210219
child.events.onAddedToGroup.dispatch(child, this);
211220
}
212-
}
213221

214-
if (this.cursor === null)
215-
{
216-
this.cursor = child;
222+
if (this.cursor === null)
223+
{
224+
this.cursor = child;
225+
}
217226
}
218227

219228
return child;
@@ -263,6 +272,8 @@ Phaser.Group.prototype.create = function (x, y, key, frame, exists) {
263272
child.alive = exists;
264273

265274
this.addChild(child);
275+
276+
child.z = this.children.length;
266277

267278
if (child.events)
268279
{
@@ -316,6 +327,23 @@ Phaser.Group.prototype.createMultiple = function (quantity, key, frame, exists)
316327

317328
}
318329

330+
/**
331+
* Internal method that re-applies all of the childrens Z values.
332+
*
333+
* @method Phaser.Group#updateZ
334+
* @protected
335+
*/
336+
Phaser.Group.prototype.updateZ = function () {
337+
338+
var i = this.children.length;
339+
340+
while (i--)
341+
{
342+
this.children[i].z = i;
343+
}
344+
345+
}
346+
319347
/**
320348
* Advances the Group cursor to the next object in the Group. If it's at the end of the Group it wraps around to the first object.
321349
*
@@ -374,7 +402,14 @@ Phaser.Group.prototype.previous = function () {
374402
*/
375403
Phaser.Group.prototype.swap = function (child1, child2) {
376404

377-
return this.swapChildren(child1, child2);
405+
var result = this.swapChildren(child1, child2);
406+
407+
if (result)
408+
{
409+
this.updateZ();
410+
}
411+
412+
return result;
378413

379414
}
380415

@@ -494,11 +529,12 @@ Phaser.Group.prototype.xy = function (index, x, y) {
494529
Phaser.Group.prototype.reverse = function () {
495530

496531
this.children.reverse();
532+
this.updateZ();
497533

498534
}
499535

500536
/**
501-
* Get the index position of the given child in this Group.
537+
* Get the index position of the given child in this Group. This should always match the childs z property.
502538
*
503539
* @method Phaser.Group#getIndex
504540
* @param {*} child - The child to get the index for.
@@ -515,7 +551,7 @@ Phaser.Group.prototype.getIndex = function (child) {
515551
*
516552
* @method Phaser.Group#replace
517553
* @param {*} oldChild - The child in this Group that will be replaced.
518-
* @param {*} newChild - The child to be inserted into this group.
554+
* @param {*} newChild - The child to be inserted into this Group.
519555
*/
520556
Phaser.Group.prototype.replace = function (oldChild, newChild) {
521557

@@ -527,17 +563,16 @@ Phaser.Group.prototype.replace = function (oldChild, newChild) {
527563
{
528564
newChild.events.onRemovedFromGroup.dispatch(newChild, this);
529565
newChild.parent.removeChild(newChild);
566+
567+
if (newChild.parent instanceof Phaser.Group)
568+
{
569+
newChild.parent.updateZ();
570+
}
530571
}
531572

532573
this.removeChild(oldChild);
533-
this.addChildAt(newChild, index);
534-
535-
newChild.events.onAddedToGroup.dispatch(newChild, this);
536574

537-
if (this.cursor === oldChild)
538-
{
539-
this.cursor = newChild;
540-
}
575+
this.addAt(newChild, index);
541576
}
542577

543578
}
@@ -1012,7 +1047,7 @@ Phaser.Group.prototype.forEachDead = function (callback, callbackContext) {
10121047
* For example to depth sort Sprites for Zelda-style game you might call `group.sort('y', Phaser.Group.SORT_ASCENDING)` at the bottom of your `State.update()`.
10131048
*
10141049
* @method Phaser.Group#sort
1015-
* @param {string} [index='y'] - The `string` name of the property you want to sort on.
1050+
* @param {string} [index='z'] - The `string` name of the property you want to sort on. Defaults to the objects z-depth value.
10161051
* @param {number} [order=Phaser.Group.SORT_ASCENDING] - The `Group` constant that defines the sort order. Possible values are Phaser.Group.SORT_ASCENDING and Phaser.Group.SORT_DESCENDING.
10171052
*/
10181053
Phaser.Group.prototype.sort = function (index, order) {
@@ -1028,10 +1063,6 @@ Phaser.Group.prototype.sort = function (index, order) {
10281063

10291064
this._sortProperty = index;
10301065

1031-
this.children.sort(this.ascendingSortHandler.bind(this));
1032-
1033-
1034-
/*
10351066
if (order === Phaser.Group.SORT_ASCENDING)
10361067
{
10371068
this.children.sort(this.ascendingSortHandler.bind(this));
@@ -1040,7 +1071,6 @@ Phaser.Group.prototype.sort = function (index, order) {
10401071
{
10411072
this.children.sort(this.descendingSortHandler.bind(this));
10421073
}
1043-
*/
10441074

10451075
}
10461076

@@ -1053,17 +1083,24 @@ Phaser.Group.prototype.sort = function (index, order) {
10531083
*/
10541084
Phaser.Group.prototype.ascendingSortHandler = function (a, b) {
10551085

1056-
if (a[this._sortProperty] < b[this._sortProperty] && this.getIndex(a) > this.getIndex(b))
1086+
if (a[this._sortProperty] < b[this._sortProperty])
10571087
{
10581088
return -1;
10591089
}
1060-
else if (a[this._sortProperty] > b[this._sortProperty] && this.getIndex(a) < this.getIndex(b))
1090+
else if (a[this._sortProperty] > b[this._sortProperty])
10611091
{
10621092
return 1;
10631093
}
10641094
else
10651095
{
1066-
return 0;
1096+
if (a.z < b.z)
1097+
{
1098+
return -1;
1099+
}
1100+
else
1101+
{
1102+
return 1;
1103+
}
10671104
}
10681105

10691106
}
@@ -1077,19 +1114,18 @@ Phaser.Group.prototype.ascendingSortHandler = function (a, b) {
10771114
*/
10781115
Phaser.Group.prototype.descendingSortHandler = function (a, b) {
10791116

1080-
if (a[this._sortProperty] && b[this._sortProperty])
1117+
if (a[this._sortProperty] < b[this._sortProperty])
10811118
{
1082-
if (a[this._sortProperty] < b[this._sortProperty])
1083-
{
1084-
return 1;
1085-
}
1086-
else if (a[this._sortProperty] > b[this._sortProperty])
1087-
{
1088-
return -1;
1089-
}
1119+
return 1;
1120+
}
1121+
else if (a[this._sortProperty] > b[this._sortProperty])
1122+
{
1123+
return -1;
1124+
}
1125+
else
1126+
{
1127+
return 0;
10901128
}
1091-
1092-
return 0;
10931129

10941130
}
10951131

@@ -1194,6 +1230,36 @@ Phaser.Group.prototype.getFirstDead = function () {
11941230

11951231
}
11961232

1233+
/**
1234+
* Returns the child at the top of this Group. The top is the one being displayed (rendered) above every other child.
1235+
*
1236+
* @method Phaser.Group#getTop
1237+
* @return {Any} The child at the top of the Group.
1238+
*/
1239+
Phaser.Group.prototype.getTop = function () {
1240+
1241+
if (this.children.length > 0)
1242+
{
1243+
return this.children[this.children.length - 1];
1244+
}
1245+
1246+
}
1247+
1248+
/**
1249+
* Returns the child at the bottom of this Group. The bottom is the one being displayed (rendered) below every other child.
1250+
*
1251+
* @method Phaser.Group#getBottom
1252+
* @return {Any} The child at the bottom of the Group.
1253+
*/
1254+
Phaser.Group.prototype.getBottom = function () {
1255+
1256+
if (this.children.length > 0)
1257+
{
1258+
return this.children[0];
1259+
}
1260+
1261+
}
1262+
11971263
/**
11981264
* Call this function to find out how many members of the group are alive.
11991265
*
@@ -1261,6 +1327,8 @@ Phaser.Group.prototype.remove = function (child) {
12611327

12621328
this.removeChild(child);
12631329

1330+
this.updateZ();
1331+
12641332
if (this.cursor === child)
12651333
{
12661334
this.next();
@@ -1332,6 +1400,8 @@ Phaser.Group.prototype.removeBetween = function (startIndex, endIndex) {
13321400
}
13331401
}
13341402

1403+
this.updateZ();
1404+
13351405
}
13361406

13371407
/**

src/gameobjects/Image.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ Phaser.Image = function (game, x, y, key, frame) {
4747
*/
4848
this.type = Phaser.IMAGE;
4949

50+
/**
51+
* @property {number} z - The z-depth value of this object within its Group (remember the World is a Group as well). No two objects in a Group can have the same z value.
52+
*/
53+
this.z = 0;
54+
5055
/**
5156
* @property {Phaser.Events} events - The Events you can subscribe to that are dispatched when certain things happen on this Image or its components.
5257
*/

0 commit comments

Comments
 (0)