Skip to content

Commit

Permalink
Group.customSort allows you to sort the Group children based on your …
Browse files Browse the repository at this point in the history
…own sort function.
  • Loading branch information
photonstorm committed Apr 9, 2014
1 parent be52515 commit 770ced8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ New Features
* Emitter.setAlpha allows you to quickly set the min and max alpha values.
* Emitter.setScale allows you to quickly set the min and max scale values.
* Emitter.blendMode lets you set the blendMode of any emitted Particle (needs a browser that supports canvas blend modes)
* Group.customSort allows you to sort the Group children based on your own sort function.


Bug Fixes
Expand Down
22 changes: 22 additions & 0 deletions src/core/Group.js
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,28 @@ Phaser.Group.prototype.sort = function (index, order) {

};

/**
* This allows you to use your own sort handler function.
* It will be sent two parameters: the two children involved in the comparison (a and b). It should return -1 if a > b, 1 if a < b or 0 if a === b.
*
* @method Phaser.Group#customSort
* @param {function} sortHandler - Your sort handler function. It will be sent two parameters: the two children involved in the comparison. It must return -1, 1 or 0.
* @param {object} context - The scope in which the sortHandler is called.
*/
Phaser.Group.prototype.customSort = function (sortHandler, context) {

if (this.children.length < 2)
{
// Nothing to swap
return;
}

this.children.sort(sortHandler.bind(context));

this.updateZ();

};

/**
* An internal helper function for the sort process.
*
Expand Down

0 comments on commit 770ced8

Please sign in to comment.