Skip to content

Instantly share code, notes, and snippets.

@in1004kyu
Last active April 7, 2017 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save in1004kyu/e770c90cd37bd73b4443c1c85cb2d6bc to your computer and use it in GitHub Desktop.
Save in1004kyu/e770c90cd37bd73b4443c1c85cb2d6bc to your computer and use it in GitHub Desktop.
Refer Phaser Group that allows for depth sorting and multiple groups
/**
*
* Example
* object_group = Playground.game.add.group(game.world, "group_all");
* order_item = new ReferGroup("item", object_group);
* order_hero = new ReferGroup("hero", object_group);
*
* // add sprite
* order_hero.add(sprite);
* // sort
* object_group.sort('y', Phaser.Group.SORT_ASCENDING);
*/
ReferGroup = {};
ReferGroup = function(name, parentGroup) {
this.name = name;
this.parentGroup = parentGroup;
this.children = [];
};
ReferGroup.prototype.name = "";
ReferGroup.prototype.parentGroup = null;
ReferGroup.prototype.children = [];
ReferGroup.prototype.add = function(sprite) {
this.parentGroup.add(sprite);
this.children.push(sprite);
};
ReferGroup.prototype.addChild = function(sprite) {
this.add(sprite);
};
ReferGroup.prototype.removeChildren = function() {
for (var i = this.children.length; i > 0; i--) {
sprite = this.children.pop();
sprite.kill();
}
this.children = [];
};
ReferGroup.prototype.removeAll = function() {
this.removeChildren();
};
ReferGroup.prototype.removeChildAt = function(index) {
sprite = this.children.splice(index, 1)[0];
sprite.kill();
};
ReferGroup.prototype.alpha = function(alpha_n) {
for (var i = 0; i < this.children.length; i++) {
this.children[i].alpha(alpha_n);
}
};
ReferGroup.prototype.bringToTop = function() {
for (var i = 0; i < this.children.length; i++) {
this.parentGroup.bringToTop(this.children[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment