Skip to content

Commit 33258a2

Browse files
committed
Added DisplayList.sortGameObjects and getTopGameObject methods which will sort a given array of game objects into display list order, factoring in the z-index as well.
1 parent f556e8b commit 33258a2

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: 'c4ee8ed0-6c88-11e7-9662-435d7268dd6f'
2+
build: '98dca120-6d40-11e7-8ba2-2f8841c28191'
33
};
44
module.exports = CHECKSUM;

v3/src/plugins/DisplayList.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,45 @@ var DisplayList = new Class({
8383
return this.list.indexOf(child);
8484
},
8585

86+
// Given an array of Game Objects, sort the array and return it,
87+
// so that the objects are in index order with the lowest at the bottom.
88+
sortGameObjects: function (gameObjects)
89+
{
90+
if (gameObjects === undefined) { gameObjects = this.list; }
91+
92+
this.scene.sys.depthSort();
93+
94+
return gameObjects.sort(this.sortIndexHandler.bind(this));
95+
},
96+
97+
getTopGameObject: function (gameObjects)
98+
{
99+
this.sortGameObjects(gameObjects);
100+
101+
return gameObjects[gameObjects.length - 1];
102+
},
103+
104+
// Return the child lowest down the display list (with the smallest index)
105+
sortIndexHandler: function (childA, childB)
106+
{
107+
// The lower the index, the lower down the display list they are
108+
var indexA = this.getIndex(childA);
109+
var indexB = this.getIndex(childB);
110+
111+
if (indexA < indexB)
112+
{
113+
return -1;
114+
}
115+
else if (indexA > indexB)
116+
{
117+
return 1;
118+
}
119+
120+
// Technically this shouldn't happen, but if the GO wasn't part of this display list then it'll
121+
// have an index of -1, so in some cases it can
122+
return 0;
123+
},
124+
86125
/**
87126
* Gets the first item from the set based on the property strictly equaling the value given.
88127
* Returns null if not found.

0 commit comments

Comments
 (0)