Added Group.sort, can sort the Group based on any given numeric property (x, y, health), finally you can do depth-sorting :)

This commit is contained in:
photonstorm 2013-11-07 04:31:37 +00:00
parent 789f0bb0a1
commit 42cd8bd812
5 changed files with 63 additions and 111 deletions

View file

@ -47,8 +47,10 @@ Version 1.1.3 - in build
* New: World.visible boolean added, toggles rendering of the world on/off entirely.
* New: Polygon class & drawPolygon method added to Graphics (thanks rjimenezda)
* New: Added Group.iterate, a powerful way to count or return child that match a certain criteria. Refactored Group to use iterate, lots of repeated code cut.
* New: Added Group.sort, can sort the Group based on any given numeric property (x, y, health), finally you can do depth-sorting :)
* Fixed: Mouse.stop now uses the true useCapture, which means the event listeners stop listening correctly (thanks beeglebug)
* Fixed: Input Keyboard example fix (thanks Atrodilla)
* Fixed: BitmapText.destroy now checks if it has a canvas before calling parentNode on it.
* Updated: ArcadePhysics.updateMotion applies the dt to the velocity calculations as well as position now (thanks jcs)
* Updated: RequestAnimationFrame now retains the callbackID which is passed to cancelRequestAnimationFrame.
* Updated: Button now goes back to over state when setFrames used in action (thanks beeglebug)

View file

@ -1,5 +1,5 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create,update:update});
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
@ -110,4 +110,13 @@ function update() {
jumpTimer = game.time.now + 750;
}
// player.scale.x += 0.001;
// player.scale.y += 0.001;
}
function render () {
game.debug.renderSpriteBody(player);
}

View file

@ -23,10 +23,13 @@ function create() {
sprite = group.create(300, 200, 'phaser');
sprite.name = 'phaser-dude';
for (var i = 0; i < 10; i++)
var names = [ 'cherries', 'orange', 'swede', 'apple', 'pepper', 'dick', 'carrot', 'cucum', 'strawb', 'broc', 'pineapl', 'melon', 'white', 'spud', 'banana', 'lettuce'];
for (var i = 0; i < names.length - 1; i++)
{
var c = group.create(game.world.randomX, game.world.randomY, 'veggies', game.rnd.integerInRange(0, 36));
c.name = 'veg' + i;
// var c = group.create(game.world.randomX, game.world.randomY, 'veggies', game.rnd.integerInRange(0, 36));
var c = group.create(game.world.randomX, game.world.randomY, 'veggies', i);
c.name = names[i];
}
game.input.onUp.add(sortGroup, this);

View file

@ -89,18 +89,6 @@ Phaser.Group = function (game, parent, name, useStage) {
*/
this.cursor = null;
/**
* Helper for sort.
*/
this._sortIndex = '';
/**
* Helper for sort.
*/
this._sortOrder = 0;
this._sortCache = [];
};
/**
@ -373,8 +361,19 @@ Phaser.Group.prototype = {
},
swapIndex: function (index1, index2) {
var child1 = this.getAt(index1);
var child2 = this.getAt(index2);
console.log('swapIndex ', index1, ' with ', index2);
this.swap(child1, child2);
},
/**
* Swaps the position of two children in this Group.
* Swaps the position of two children in this Group. Both children must be in this Group.
* You cannot swap a child with itself, or swap un-parented children, doing so will return false.
*
* @method Phaser.Group#swap
@ -384,11 +383,8 @@ Phaser.Group.prototype = {
*/
swap: function (child1, child2) {
console.log('starting swap', child1.name, 'with', child2.name);
if (child1 === child2 || !child1.parent || !child2.parent)
if (child1 === child2 || !child1.parent || !child2.parent || child1.group !== this || child2.group !== this)
{
console.log('aborting');
return false;
}
@ -398,17 +394,8 @@ Phaser.Group.prototype = {
var child2Prev = child2._iPrev;
var child2Next = child2._iNext;
// var endNode = this._container.last._iNext;
var endNode = this._container.last;
var endNode = this._container.last._iNext;
var currentNode = this.game.stage._stage;
// console.log('start do while. start node: ', currentNode.name);
// console.log(typeof endNode);
// if (endNode)
// {
// console.log('end node: ', endNode.name);
// }
do
{
@ -440,10 +427,6 @@ Phaser.Group.prototype = {
if (child1._iNext == child2)
{
// This is a downward (A to B) neighbour swap
// console.log('downward A to B');
// this.childTest('1', child1);
// this.childTest('2', child2);
child1._iNext = child2Next;
child1._iPrev = child2;
child2._iNext = child1;
@ -467,10 +450,6 @@ Phaser.Group.prototype = {
else if (child2._iNext == child1)
{
// This is an upward (B to A) neighbour swap
// console.log('upward B to A');
// this.childTest('1', child1);
// this.childTest('2', child2);
child1._iNext = child2;
child1._iPrev = child2Prev;
child2._iNext = child1Next;
@ -494,11 +473,6 @@ Phaser.Group.prototype = {
else
{
// Children are far apart
// console.log('far apart A to B');
// this.childTest('1', child1);
// this.childTest('2', child2);
child1._iNext = child2Next;
child1._iPrev = child2Prev;
child2._iNext = child1Next;
@ -1024,43 +998,43 @@ Phaser.Group.prototype = {
if (typeof index === 'undefined') { index = 'y'; }
if (typeof order === 'undefined') { order = Phaser.Group.SORT_ASCENDING; }
this._sortIndex = index;
this._sortOrder = order;
this._sortCache = this._container.children.slice();
console.log('-vvv--------------------------------------------------------------------------------');
this.dump(true);
for (var i = 0; i < this._sortCache.length; i++)
{
console.log(i + ' = ' + this._sortCache[i].name + ' at y: ' + this._sortCache[i].y);
}
var swapped;
var temp;
console.log('---------------------------------------------------------------------------------');
do {
this._sortCache.sort(this.sortHandler.bind(this));
swapped = false;
// Should do an array compare here, no need to rebuild the display list if the arrays are the same
for (var i = 0; i < this._sortCache.length; i++)
{
console.log(i + ' = ' + this._sortCache[i].name + ' at y: ' + this._sortCache[i].y);
}
for (var i = 0; i < this._sortCache.length; i++)
{
// if (this._container.children[i] !== this._sortCache[i])
// {
// console.log('swapped:', this._container.children[i].name,'with',this._sortCache[i].name);
this.swap(this._container.children[i], this._sortCache[i]);
// }
}
// Now put it back again
this._container.children = this._sortCache.slice();
this._container.updateTransform();
for (var i = 0, len = this._container.children.length - 1; i < len; i++)
{
if (order == Phaser.Group.SORT_ASCENDING)
{
if (this._container.children[i][index] > this._container.children[i + 1][index])
{
this.swap(this.getAt(i), this.getAt(i + 1));
temp = this._container.children[i];
this._container.children[i] = this._container.children[i + 1];
this._container.children[i + 1] = temp;
swapped = true;
}
}
else
{
if (this._container.children[i][index] < this._container.children[i + 1][index])
{
this.swap(this.getAt(i), this.getAt(i + 1));
temp = this._container.children[i];
this._container.children[i] = this._container.children[i + 1];
this._container.children[i + 1] = temp;
swapped = true;
}
}
}
} while (swapped);
this.dump(true);
@ -1068,40 +1042,6 @@ Phaser.Group.prototype = {
},
/**
* Helper function for the sort process.
*
* @param {Basic} Obj1 The first object being sorted.
* @param {Basic} Obj2 The second object being sorted.
*
* @return {number} An integer value: -1 (Obj1 before Obj2), 0 (same), or 1 (Obj1 after Obj2).
*/
sortHandler: function (obj1, obj2) {
if (!obj1 || !obj2)
{
// console.log('null objects in sort', obj1, obj2);
return 0;
}
// number only test
// return obj1[this._sortIndex] - obj2[this._sortIndex];
if (obj1[this._sortIndex] < obj2[this._sortIndex])
{
// console.log('1 < 2');
return this._sortOrder;
}
else if (obj1[this._sortIndex] > obj2[this._sortIndex])
{
// console.log('1 > 2');
return -this._sortOrder;
}
return 0;
},
/**
* Iterates over the children of the Group. When a child has a property matching key that equals the given value, it is considered as a match.
* Matched children can be sent to the optional callback, or simply returned or counted.
@ -1441,13 +1381,11 @@ Phaser.Group.prototype = {
if (full)
{
var testObject = this.game.stage._stage.last._iNext;
// var testObject = this.game.stage._stage.last;
var displayObject = this.game.stage._stage;
}
else
{
var testObject = this._container.last._iNext;
// var testObject = this._container.last;
var displayObject = this._container;
}

View file

@ -155,7 +155,7 @@ Phaser.BitmapText.prototype.destroy = function() {
this.group.remove(this);
}
if (this.canvas.parentNode)
if (this.canvas && this.canvas.parentNode)
{
this.canvas.parentNode.removeChild(this.canvas);
}