2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2014-02-05 05:54:25 +00:00
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Phaser Group constructor.
|
|
|
|
* @class Phaser.Group
|
2014-02-14 01:09:52 +00:00
|
|
|
* @classdesc A Group is a container for display objects that allows for fast pooling and object recycling. Groups can be nested within other Groups and have their own local transforms.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
2014-07-14 11:44:12 +00:00
|
|
|
* @param {Phaser.Group|Phaser.Sprite|null} parent - The parent Group, DisplayObject or DisplayObjectContainer that this Group will be added to. If `undefined` it will use game.world. If null it won't be added to anything.
|
2013-10-02 00:51:30 +00:00
|
|
|
* @param {string} [name=group] - A name for this Group. Not used internally but useful for debugging.
|
2014-02-12 19:45:09 +00:00
|
|
|
* @param {boolean} [addToStage=false] - If set to true this Group will be added directly to the Game.Stage instead of Game.World.
|
2014-03-13 11:45:55 +00:00
|
|
|
* @param {boolean} [enableBody=false] - If true all Sprites created with `Group.create` or `Group.createMulitple` will have a physics body created on them. Change the body type with physicsBodyType.
|
|
|
|
* @param {number} [physicsBodyType=0] - If enableBody is true this is the type of physics body that is created on new Sprites. Phaser.Physics.ARCADE, Phaser.Physics.P2, Phaser.Physics.NINJA, etc.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2014-03-13 11:45:55 +00:00
|
|
|
Phaser.Group = function (game, parent, name, addToStage, enableBody, physicsBodyType) {
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2014-02-21 12:33:15 +00:00
|
|
|
if (typeof addToStage === 'undefined') { addToStage = false; }
|
2014-03-13 11:45:55 +00:00
|
|
|
if (typeof enableBody === 'undefined') { enableBody = false; }
|
|
|
|
if (typeof physicsBodyType === 'undefined') { physicsBodyType = Phaser.Physics.ARCADE; }
|
2014-02-21 12:33:15 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - A reference to the currently running Game.
|
|
|
|
*/
|
|
|
|
this.game = game;
|
2013-12-06 01:07:25 +00:00
|
|
|
|
2014-02-21 12:33:15 +00:00
|
|
|
if (typeof parent === 'undefined')
|
2013-12-06 01:07:25 +00:00
|
|
|
{
|
|
|
|
parent = game.world;
|
|
|
|
}
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {string} name - A name for this Group. Not used internally but useful for debugging.
|
|
|
|
*/
|
|
|
|
this.name = name || 'group';
|
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
PIXI.DisplayObjectContainer.call(this);
|
|
|
|
|
2014-02-21 12:33:15 +00:00
|
|
|
if (addToStage)
|
|
|
|
{
|
|
|
|
this.game.stage.addChild(this);
|
|
|
|
}
|
|
|
|
else
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
|
|
|
if (parent)
|
|
|
|
{
|
2014-02-06 03:34:27 +00:00
|
|
|
parent.addChild(this);
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2014-02-06 03:34:27 +00:00
|
|
|
}
|
2013-09-11 12:21:07 +00:00
|
|
|
|
2014-03-10 23:01:10 +00:00
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
this.z = 0;
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* @property {number} type - Internal Phaser Type value.
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
this.type = Phaser.GROUP;
|
|
|
|
|
2014-01-06 01:39:23 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} alive - The alive property is useful for Groups that are children of other Groups and need to be included/excluded in checks like forEachAlive.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.alive = true;
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-02-06 02:31:36 +00:00
|
|
|
* @property {boolean} exists - If exists is true the Group is updated, otherwise it is skipped.
|
2013-11-25 03:13:04 +00:00
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.exists = true;
|
2013-10-04 15:51:24 +00:00
|
|
|
|
2014-04-23 20:49:18 +00:00
|
|
|
/**
|
|
|
|
* The type of objects that will be created when you use Group.create or Group.createMultiple. Defaults to Phaser.Sprite.
|
|
|
|
* When a new object is created it is passed the following parameters to its constructor: game, x, y, key, frame.
|
|
|
|
* @property {object} classType
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.classType = Phaser.Sprite;
|
|
|
|
|
2014-01-06 01:39:23 +00:00
|
|
|
/**
|
2014-02-06 13:15:45 +00:00
|
|
|
* @property {Phaser.Group|Phaser.Sprite} parent - The parent of this Group.
|
2014-01-06 01:39:23 +00:00
|
|
|
*/
|
|
|
|
|
2013-10-04 15:51:24 +00:00
|
|
|
/**
|
2014-02-06 03:34:27 +00:00
|
|
|
* @property {Phaser.Point} scale - The scale of the Group container.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-02-06 03:34:27 +00:00
|
|
|
this.scale = new Phaser.Point(1, 1);
|
2013-10-04 15:51:24 +00:00
|
|
|
|
2014-02-01 04:54:02 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Point} pivot - The pivot point of the Group container.
|
|
|
|
*/
|
|
|
|
|
2013-11-01 02:07:21 +00:00
|
|
|
/**
|
2013-11-01 02:50:00 +00:00
|
|
|
* The cursor is a simple way to iterate through the objects in a Group using the Group.next and Group.previous functions.
|
|
|
|
* The cursor is set to the first child added to the Group and doesn't change unless you call next, previous or set it directly with Group.cursor.
|
|
|
|
* @property {any} cursor - The current display object that the Group cursor is pointing to.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2013-11-01 02:07:21 +00:00
|
|
|
this.cursor = null;
|
|
|
|
|
2014-02-26 02:45:06 +00:00
|
|
|
/**
|
2014-03-10 03:06:28 +00:00
|
|
|
* @property {Phaser.Point} cameraOffset - If this object is fixedToCamera then this stores the x/y offset that its drawn at, from the top-left of the camera view.
|
2014-02-26 02:45:06 +00:00
|
|
|
*/
|
2014-03-10 03:06:28 +00:00
|
|
|
this.cameraOffset = new Phaser.Point();
|
2014-02-06 02:31:36 +00:00
|
|
|
|
2014-02-15 01:27:42 +00:00
|
|
|
/**
|
2014-03-14 03:26:06 +00:00
|
|
|
* @property {boolean} enableBody - If true all Sprites created by, or added to this Group, will have a physics body enabled on them. Change the body type with `Group.physicsBodyType`.
|
2014-03-13 07:29:23 +00:00
|
|
|
* @default
|
2014-02-15 01:27:42 +00:00
|
|
|
*/
|
2014-03-16 00:45:36 +00:00
|
|
|
this.enableBody = enableBody;
|
2014-03-10 03:06:28 +00:00
|
|
|
|
2014-03-13 07:29:23 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} enableBodyDebug - If true when a physics body is created (via Group.enableBody) it will create a physics debug object as well. Only works for P2 bodies.
|
|
|
|
*/
|
2014-03-16 00:45:36 +00:00
|
|
|
this.enableBodyDebug = false;
|
2014-03-13 07:29:23 +00:00
|
|
|
|
2014-03-10 03:06:28 +00:00
|
|
|
/**
|
|
|
|
* @property {number} physicsBodyType - If Group.enableBody is true this is the type of physics body that is created on new Sprites. Phaser.Physics.ARCADE, Phaser.Physics.P2, Phaser.Physics.NINJA, etc.
|
|
|
|
*/
|
2014-03-13 11:45:55 +00:00
|
|
|
this.physicsBodyType = physicsBodyType;
|
2014-02-15 01:27:42 +00:00
|
|
|
|
2014-03-10 14:33:18 +00:00
|
|
|
/**
|
|
|
|
* @property {string} _sortProperty - The property on which children are sorted.
|
|
|
|
* @private
|
|
|
|
*/
|
2014-03-10 23:01:10 +00:00
|
|
|
this._sortProperty = 'z';
|
2014-03-10 14:33:18 +00:00
|
|
|
|
2014-02-15 01:27:42 +00:00
|
|
|
/**
|
|
|
|
* A small internal cache:
|
|
|
|
* 0 = previous position.x
|
|
|
|
* 1 = previous position.y
|
|
|
|
* 2 = previous rotation
|
|
|
|
* 3 = renderID
|
|
|
|
* 4 = fresh? (0 = no, 1 = yes)
|
|
|
|
* 5 = outOfBoundsFired (0 = no, 1 = yes)
|
|
|
|
* 6 = exists (0 = no, 1 = yes)
|
|
|
|
* 7 = fixed to camera (0 = no, 1 = yes)
|
2014-03-10 03:06:28 +00:00
|
|
|
* 8 = cursor index
|
2014-03-10 14:33:18 +00:00
|
|
|
* 9 = sort order
|
2014-03-19 12:05:19 +00:00
|
|
|
* @property {Array} _cache
|
2014-02-15 01:27:42 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-03-19 12:05:19 +00:00
|
|
|
this._cache = [ 0, 0, 0, 0, 1, 0, 1, 0, 0, 0 ];
|
2014-02-15 01:27:42 +00:00
|
|
|
|
2013-09-05 20:07:44 +00:00
|
|
|
};
|
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
Phaser.Group.prototype = Object.create(PIXI.DisplayObjectContainer.prototype);
|
|
|
|
Phaser.Group.prototype.constructor = Phaser.Group;
|
|
|
|
|
2013-11-06 16:46:21 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
Phaser.Group.RETURN_NONE = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
Phaser.Group.RETURN_TOTAL = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
Phaser.Group.RETURN_CHILD = 2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
Phaser.Group.SORT_ASCENDING = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
Phaser.Group.SORT_DESCENDING = 1;
|
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Adds an existing object to this Group. The object can be an instance of Phaser.Sprite, Phaser.Button or any other display object.
|
|
|
|
* The child is automatically added to the top of the Group, so renders on-top of everything else within the Group. If you need to control
|
|
|
|
* that then see the addAt method.
|
|
|
|
*
|
|
|
|
* @see Phaser.Group#create
|
|
|
|
* @see Phaser.Group#addAt
|
|
|
|
* @method Phaser.Group#add
|
|
|
|
* @param {*} child - An instance of Phaser.Sprite, Phaser.Button or any other display object..
|
2014-07-02 13:04:14 +00:00
|
|
|
* @param {boolean} [silent=false] - If the silent parameter is `true` the child will not dispatch the onAddedToGroup event.
|
2014-02-06 02:31:36 +00:00
|
|
|
* @return {*} The child that was added to the Group.
|
|
|
|
*/
|
2014-07-02 13:04:14 +00:00
|
|
|
Phaser.Group.prototype.add = function (child, silent) {
|
|
|
|
|
|
|
|
if (typeof silent === 'undefined') { silent = false; }
|
2014-02-06 02:31:36 +00:00
|
|
|
|
2014-02-06 13:15:45 +00:00
|
|
|
if (child.parent !== this)
|
2014-02-06 02:31:36 +00:00
|
|
|
{
|
2014-03-14 03:26:06 +00:00
|
|
|
if (this.enableBody)
|
|
|
|
{
|
|
|
|
this.game.physics.enable(child, this.physicsBodyType);
|
|
|
|
}
|
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
this.addChild(child);
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-03-10 23:01:10 +00:00
|
|
|
child.z = this.children.length;
|
|
|
|
|
2014-07-02 13:04:14 +00:00
|
|
|
if (!silent && child.events)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-02-06 02:31:36 +00:00
|
|
|
child.events.onAddedToGroup.dispatch(child, this);
|
|
|
|
}
|
2014-01-06 01:39:23 +00:00
|
|
|
|
2014-03-10 23:01:10 +00:00
|
|
|
if (this.cursor === null)
|
|
|
|
{
|
|
|
|
this.cursor = child;
|
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2014-01-06 01:39:23 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
return child;
|
2014-01-06 01:39:23 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2014-02-01 04:54:02 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Adds an existing object to this Group. The object can be an instance of Phaser.Sprite, Phaser.Button or any other display object.
|
|
|
|
* The child is added to the Group at the location specified by the index value, this allows you to control child ordering.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#addAt
|
|
|
|
* @param {*} child - An instance of Phaser.Sprite, Phaser.Button or any other display object..
|
|
|
|
* @param {number} index - The index within the Group to insert the child to.
|
2014-07-02 13:04:14 +00:00
|
|
|
* @param {boolean} [silent=false] - If the silent parameter is `true` the child will not dispatch the onAddedToGroup event.
|
2014-02-06 02:31:36 +00:00
|
|
|
* @return {*} The child that was added to the Group.
|
|
|
|
*/
|
2014-07-02 13:04:14 +00:00
|
|
|
Phaser.Group.prototype.addAt = function (child, index, silent) {
|
|
|
|
|
|
|
|
if (typeof silent === 'undefined') { silent = false; }
|
2014-02-01 04:54:02 +00:00
|
|
|
|
2014-02-06 13:15:45 +00:00
|
|
|
if (child.parent !== this)
|
2014-02-06 02:31:36 +00:00
|
|
|
{
|
2014-03-14 03:26:06 +00:00
|
|
|
if (this.enableBody)
|
|
|
|
{
|
|
|
|
this.game.physics.enable(child, this.physicsBodyType);
|
|
|
|
}
|
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
this.addChildAt(child, index);
|
|
|
|
|
2014-03-10 23:01:10 +00:00
|
|
|
this.updateZ();
|
|
|
|
|
2014-07-02 13:04:14 +00:00
|
|
|
if (!silent && child.events)
|
2014-02-06 02:31:36 +00:00
|
|
|
{
|
|
|
|
child.events.onAddedToGroup.dispatch(child, this);
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-03-10 23:01:10 +00:00
|
|
|
if (this.cursor === null)
|
|
|
|
{
|
|
|
|
this.cursor = child;
|
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
return child;
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Returns the child found at the given index within this Group.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#getAt
|
|
|
|
* @param {number} index - The index to return the child from.
|
2014-02-28 18:55:07 +00:00
|
|
|
* @return {*} The child that was found at the given index. If the index was out of bounds then this will return -1.
|
2014-02-06 02:31:36 +00:00
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.getAt = function (index) {
|
2014-01-06 01:39:23 +00:00
|
|
|
|
2014-03-15 23:53:05 +00:00
|
|
|
if (index < 0 || index >= this.children.length)
|
2014-02-28 18:55:07 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return this.getChildAt(index);
|
|
|
|
}
|
2014-01-06 01:39:23 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-09-12 03:24:01 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Automatically creates a new Phaser.Sprite object and adds it to the top of this Group.
|
2014-04-23 20:49:18 +00:00
|
|
|
* You can change Group.classType to any object and this call will create an object of that type instead, but it should extend either Sprite or Image.
|
2014-02-06 02:31:36 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Group#create
|
|
|
|
* @param {number} x - The x coordinate to display the newly created Sprite at. The value is in relation to the Group.x point.
|
|
|
|
* @param {number} y - The y coordinate to display the newly created Sprite at. The value is in relation to the Group.y point.
|
|
|
|
* @param {string} key - The Game.cache key of the image that this Sprite will use.
|
|
|
|
* @param {number|string} [frame] - If the Sprite image contains multiple frames you can specify which one to use here.
|
|
|
|
* @param {boolean} [exists=true] - The default exists state of the Sprite.
|
2014-04-23 20:49:18 +00:00
|
|
|
* @return {Phaser.Sprite|object} The child that was created. Will be a Phaser.Sprite unless Group.classType has been changed.
|
2014-02-06 02:31:36 +00:00
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.create = function (x, y, key, frame, exists) {
|
2014-02-01 04:54:02 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (typeof exists === 'undefined') { exists = true; }
|
2014-02-01 04:54:02 +00:00
|
|
|
|
2014-04-23 20:49:18 +00:00
|
|
|
var child = new this.classType(this.game, x, y, key, frame);
|
2013-11-01 02:50:00 +00:00
|
|
|
|
2014-03-13 10:14:06 +00:00
|
|
|
if (this.enableBody)
|
|
|
|
{
|
2014-03-14 03:26:06 +00:00
|
|
|
this.game.physics.enable(child, this.physicsBodyType);
|
2014-03-13 10:14:06 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
child.exists = exists;
|
|
|
|
child.visible = exists;
|
|
|
|
child.alive = exists;
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
this.addChild(child);
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-03-10 23:01:10 +00:00
|
|
|
child.z = this.children.length;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (child.events)
|
|
|
|
{
|
|
|
|
child.events.onAddedToGroup.dispatch(child, this);
|
|
|
|
}
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (this.cursor === null)
|
|
|
|
{
|
|
|
|
this.cursor = child;
|
|
|
|
}
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
return child;
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Automatically creates multiple Phaser.Sprite objects and adds them to the top of this Group.
|
|
|
|
* Useful if you need to quickly generate a pool of identical sprites, such as bullets. By default the sprites will be set to not exist
|
|
|
|
* and will be positioned at 0, 0 (relative to the Group.x/y)
|
2014-04-23 20:49:18 +00:00
|
|
|
* You can change Group.classType to any object and this call will create an object of that type instead, but it should extend either Sprite or Image.
|
2014-02-06 02:31:36 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Group#createMultiple
|
|
|
|
* @param {number} quantity - The number of Sprites to create.
|
|
|
|
* @param {string} key - The Game.cache key of the image that this Sprite will use.
|
|
|
|
* @param {number|string} [frame] - If the Sprite image contains multiple frames you can specify which one to use here.
|
|
|
|
* @param {boolean} [exists=false] - The default exists state of the Sprite.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.createMultiple = function (quantity, key, frame, exists) {
|
2013-09-14 23:14:45 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (typeof exists === 'undefined') { exists = false; }
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
for (var i = 0; i < quantity; i++)
|
|
|
|
{
|
2014-02-06 13:15:45 +00:00
|
|
|
this.create(0, 0, key, frame, exists);
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-10-08 20:09:46 +00:00
|
|
|
|
2014-03-10 23:01:10 +00:00
|
|
|
/**
|
|
|
|
* Internal method that re-applies all of the childrens Z values.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#updateZ
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.updateZ = function () {
|
|
|
|
|
|
|
|
var i = this.children.length;
|
|
|
|
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
this.children[i].z = i;
|
|
|
|
}
|
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2014-03-10 23:01:10 +00:00
|
|
|
|
2014-04-16 20:39:04 +00:00
|
|
|
/**
|
|
|
|
* Sets the Group cursor to the first object in the Group. If the optional index parameter is given it sets the cursor to the object at that index instead.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#resetCursor
|
|
|
|
* @param {number} [index=0] - Set the cursor to point to a specific index.
|
|
|
|
* @return {*} The child the cursor now points to.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.resetCursor = function (index) {
|
|
|
|
|
|
|
|
if (typeof index === 'undefined') { index = 0; }
|
|
|
|
|
|
|
|
if (index > this.children.length - 1)
|
|
|
|
{
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.cursor)
|
|
|
|
{
|
|
|
|
this._cache[8] = index;
|
|
|
|
this.cursor = this.children[this._cache[8]];
|
|
|
|
return this.cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#next
|
2014-04-02 09:40:38 +00:00
|
|
|
* @return {*} The child the cursor now points to.
|
2014-02-06 02:31:36 +00:00
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.next = function () {
|
2013-10-08 20:09:46 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (this.cursor)
|
|
|
|
{
|
|
|
|
// Wrap the cursor?
|
2014-04-03 15:42:55 +00:00
|
|
|
if (this._cache[8] >= this.children.length - 1)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-03-10 03:06:28 +00:00
|
|
|
this._cache[8] = 0;
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-10 03:06:28 +00:00
|
|
|
this._cache[8]++;
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2014-02-01 04:54:02 +00:00
|
|
|
|
2014-03-10 03:06:28 +00:00
|
|
|
this.cursor = this.children[this._cache[8]];
|
2014-04-02 09:40:38 +00:00
|
|
|
|
|
|
|
return this.cursor;
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2014-02-01 04:54:02 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-10-08 20:09:46 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Moves the Group cursor to the previous object in the Group. If it's at the start of the Group it wraps around to the last object.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#previous
|
2014-04-02 09:40:38 +00:00
|
|
|
* @return {*} The child the cursor now points to.
|
2014-02-06 02:31:36 +00:00
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.previous = function () {
|
2013-11-01 02:50:00 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (this.cursor)
|
|
|
|
{
|
|
|
|
// Wrap the cursor?
|
2014-03-10 03:06:28 +00:00
|
|
|
if (this._cache[8] === 0)
|
2014-02-06 02:31:36 +00:00
|
|
|
{
|
2014-03-10 03:06:28 +00:00
|
|
|
this._cache[8] = this.children.length - 1;
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
else
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-03-10 03:06:28 +00:00
|
|
|
this._cache[8]--;
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 03:06:28 +00:00
|
|
|
this.cursor = this.children[this._cache[8]];
|
2014-04-02 09:40:38 +00:00
|
|
|
|
|
|
|
return this.cursor;
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2013-11-01 02:50:00 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-01 02:50:00 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @param {*} child1 - The first child to swap.
|
|
|
|
* @param {*} child2 - The second child to swap.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.swap = function (child1, child2) {
|
2013-10-08 20:09:46 +00:00
|
|
|
|
2014-03-10 23:01:10 +00:00
|
|
|
var result = this.swapChildren(child1, child2);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
this.updateZ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-06 16:46:21 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Brings the given child to the top of this Group so it renders above all other children.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#bringToTop
|
|
|
|
* @param {*} child - The child to bring to the top of this Group.
|
|
|
|
* @return {*} The child that was moved.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.bringToTop = function (child) {
|
2013-11-06 16:46:21 +00:00
|
|
|
|
2014-02-28 18:55:07 +00:00
|
|
|
if (child.parent === this && this.getIndex(child) < this.children.length)
|
2014-02-06 02:31:36 +00:00
|
|
|
{
|
2014-07-02 13:04:14 +00:00
|
|
|
this.remove(child, false, true);
|
|
|
|
this.add(child, true);
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2013-11-06 16:46:21 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
return child;
|
2013-11-06 16:46:21 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-06 16:46:21 +00:00
|
|
|
|
2014-02-28 18:55:07 +00:00
|
|
|
/**
|
|
|
|
* Sends the given child to the bottom of this Group so it renders below all other children.
|
|
|
|
*
|
2014-03-03 22:43:35 +00:00
|
|
|
* @method Phaser.Group#sendToBack
|
2014-02-28 18:55:07 +00:00
|
|
|
* @param {*} child - The child to send to the bottom of this Group.
|
|
|
|
* @return {*} The child that was moved.
|
|
|
|
*/
|
2014-03-03 22:43:35 +00:00
|
|
|
Phaser.Group.prototype.sendToBack = function (child) {
|
2014-02-28 18:55:07 +00:00
|
|
|
|
|
|
|
if (child.parent === this && this.getIndex(child) > 0)
|
|
|
|
{
|
2014-07-02 13:04:14 +00:00
|
|
|
this.remove(child, false, true);
|
|
|
|
this.addAt(child, 0, true);
|
2014-02-28 18:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return child;
|
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2014-02-28 18:55:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves the given child up one place in this Group unless it's already at the top.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#moveUp
|
|
|
|
* @param {*} child - The child to move up in the Group.
|
|
|
|
* @return {*} The child that was moved.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.moveUp = function (child) {
|
|
|
|
|
|
|
|
if (child.parent === this && this.getIndex(child) < this.children.length - 1)
|
|
|
|
{
|
|
|
|
var a = this.getIndex(child);
|
|
|
|
var b = this.getAt(a + 1);
|
|
|
|
|
|
|
|
if (b)
|
|
|
|
{
|
2014-04-02 20:39:20 +00:00
|
|
|
this.swap(child, b);
|
2014-02-28 18:55:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return child;
|
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2014-02-28 18:55:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves the given child down one place in this Group unless it's already at the top.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#moveDown
|
|
|
|
* @param {*} child - The child to move down in the Group.
|
|
|
|
* @return {*} The child that was moved.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.moveDown = function (child) {
|
|
|
|
|
|
|
|
if (child.parent === this && this.getIndex(child) > 0)
|
|
|
|
{
|
|
|
|
var a = this.getIndex(child);
|
|
|
|
var b = this.getAt(a - 1);
|
|
|
|
|
|
|
|
if (b)
|
|
|
|
{
|
2014-04-02 20:39:20 +00:00
|
|
|
this.swap(child, b);
|
2014-02-28 18:55:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return child;
|
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2014-02-28 18:55:07 +00:00
|
|
|
|
2014-03-03 22:43:35 +00:00
|
|
|
/**
|
|
|
|
* Positions the child found at the given index within this Group to the given x and y coordinates.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#xy
|
|
|
|
* @param {number} index - The index of the child in the Group to set the position of.
|
|
|
|
* @param {number} x - The new x position of the child.
|
|
|
|
* @param {number} y - The new y position of the child.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.xy = function (index, x, y) {
|
|
|
|
|
|
|
|
if (index < 0 || index > this.children.length)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.getChildAt(index).x = x;
|
|
|
|
this.getChildAt(index).y = y;
|
|
|
|
}
|
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2014-03-03 22:43:35 +00:00
|
|
|
|
2014-03-04 01:27:57 +00:00
|
|
|
/**
|
|
|
|
* Reverses all children in this Group. Note that this does not propagate, only direct children are re-ordered.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#reverse
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.reverse = function () {
|
|
|
|
|
|
|
|
this.children.reverse();
|
2014-03-10 23:01:10 +00:00
|
|
|
this.updateZ();
|
2014-03-04 01:27:57 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2014-03-04 01:27:57 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
2014-03-10 23:01:10 +00:00
|
|
|
* Get the index position of the given child in this Group. This should always match the childs z property.
|
2014-02-06 02:31:36 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Group#getIndex
|
|
|
|
* @param {*} child - The child to get the index for.
|
|
|
|
* @return {number} The index of the child or -1 if it's not a member of this Group.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.getIndex = function (child) {
|
2013-11-06 16:46:21 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
return this.children.indexOf(child);
|
2013-11-06 16:46:21 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-07 04:31:37 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Replaces a child of this Group with the given newChild. The newChild cannot be a member of this Group.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#replace
|
|
|
|
* @param {*} oldChild - The child in this Group that will be replaced.
|
2014-03-10 23:01:10 +00:00
|
|
|
* @param {*} newChild - The child to be inserted into this Group.
|
2014-03-19 00:54:49 +00:00
|
|
|
* @return {*} Returns the oldChild that was replaced within this Group.
|
2014-02-06 02:31:36 +00:00
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.replace = function (oldChild, newChild) {
|
2013-11-07 04:31:37 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
var index = this.getIndex(oldChild);
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (index !== -1)
|
|
|
|
{
|
|
|
|
if (newChild.parent !== undefined)
|
|
|
|
{
|
|
|
|
newChild.events.onRemovedFromGroup.dispatch(newChild, this);
|
|
|
|
newChild.parent.removeChild(newChild);
|
2014-03-10 23:01:10 +00:00
|
|
|
|
|
|
|
if (newChild.parent instanceof Phaser.Group)
|
|
|
|
{
|
|
|
|
newChild.parent.updateZ();
|
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2013-11-07 04:31:37 +00:00
|
|
|
|
2014-03-19 00:54:49 +00:00
|
|
|
var temp = oldChild;
|
|
|
|
|
|
|
|
this.remove(temp);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-10 23:01:10 +00:00
|
|
|
this.addAt(newChild, index);
|
2014-03-19 00:54:49 +00:00
|
|
|
|
|
|
|
return temp;
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
2014-05-01 10:45:17 +00:00
|
|
|
* Checks if the child has the given property. Will scan up to 4 levels deep only.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#hasProperty
|
|
|
|
* @param {*} child - The child to check for the existance of the property on.
|
|
|
|
* @param {array} key - An array of strings that make up the property.
|
|
|
|
* @return {boolean} True if the child has the property, otherwise false.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.hasProperty = function (child, key) {
|
|
|
|
|
|
|
|
var len = key.length;
|
|
|
|
|
2014-05-19 12:11:58 +00:00
|
|
|
if (len === 1 && key[0] in child)
|
2014-05-01 10:45:17 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-19 12:11:58 +00:00
|
|
|
else if (len === 2 && key[0] in child && key[1] in child[key[0]])
|
2014-05-01 10:45:17 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-19 12:11:58 +00:00
|
|
|
else if (len === 3 && key[0] in child && key[1] in child[key[0]] && key[2] in child[key[0]][key[1]])
|
2014-05-01 10:45:17 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-19 12:11:58 +00:00
|
|
|
else if (len === 4 && key[0] in child && key[1] in child[key[0]] && key[2] in child[key[0]][key[1]] && key[3] in child[key[0]][key[1]][key[2]])
|
2014-05-01 10:45:17 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a property to the given value on the child. The operation parameter controls how the value is set.
|
|
|
|
* Operation 0 means set the existing value to the given value, or if force is `false` create a new property with the given value.
|
|
|
|
* 1 will add the given value to the value already present.
|
|
|
|
* 2 will subtract the given value from the value already present.
|
|
|
|
* 3 will multiply the value already present by the given value.
|
|
|
|
* 4 will divide the value already present by the given value.
|
2014-02-06 02:31:36 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Group#setProperty
|
|
|
|
* @param {*} child - The child to set the property value on.
|
|
|
|
* @param {array} key - An array of strings that make up the property that will be set.
|
|
|
|
* @param {*} value - The value that will be set.
|
|
|
|
* @param {number} [operation=0] - Controls how the value is assigned. A value of 0 replaces the value with the new one. A value of 1 adds it, 2 subtracts it, 3 multiplies it and 4 divides it.
|
2014-05-01 10:45:17 +00:00
|
|
|
* @param {boolean} [force=false] - If `force` is true then the property will be set on the child regardless if it already exists or not. If false and the property doesn't exist, nothing will be set.
|
|
|
|
* @return {boolean} True if the property was set, false if not.
|
2014-02-06 02:31:36 +00:00
|
|
|
*/
|
2014-05-01 10:45:17 +00:00
|
|
|
Phaser.Group.prototype.setProperty = function (child, key, value, operation, force) {
|
|
|
|
|
|
|
|
if (typeof force === 'undefined') { force = false; }
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
operation = operation || 0;
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-04-17 11:11:37 +00:00
|
|
|
// As ugly as this approach looks, and although it's limited to a depth of only 4, it's much faster than a for loop or object iteration.
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
// 0 = Equals
|
|
|
|
// 1 = Add
|
|
|
|
// 2 = Subtract
|
|
|
|
// 3 = Multiply
|
|
|
|
// 4 = Divide
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-05-01 10:45:17 +00:00
|
|
|
// We can't force a property in and the child doesn't have it, so abort.
|
|
|
|
// Equally we can't add, subtract, multiply or divide a property value if it doesn't exist, so abort in those cases too.
|
2014-05-19 12:11:58 +00:00
|
|
|
if (!this.hasProperty(child, key) && (!force || operation > 0))
|
2014-05-01 10:45:17 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
var len = key.length;
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-05-01 10:45:17 +00:00
|
|
|
if (len === 1)
|
2014-02-06 02:31:36 +00:00
|
|
|
{
|
|
|
|
if (operation === 0) { child[key[0]] = value; }
|
|
|
|
else if (operation == 1) { child[key[0]] += value; }
|
|
|
|
else if (operation == 2) { child[key[0]] -= value; }
|
|
|
|
else if (operation == 3) { child[key[0]] *= value; }
|
|
|
|
else if (operation == 4) { child[key[0]] /= value; }
|
|
|
|
}
|
2014-05-01 10:45:17 +00:00
|
|
|
else if (len === 2)
|
2014-02-06 02:31:36 +00:00
|
|
|
{
|
|
|
|
if (operation === 0) { child[key[0]][key[1]] = value; }
|
|
|
|
else if (operation == 1) { child[key[0]][key[1]] += value; }
|
|
|
|
else if (operation == 2) { child[key[0]][key[1]] -= value; }
|
|
|
|
else if (operation == 3) { child[key[0]][key[1]] *= value; }
|
|
|
|
else if (operation == 4) { child[key[0]][key[1]] /= value; }
|
|
|
|
}
|
2014-05-01 10:45:17 +00:00
|
|
|
else if (len === 3)
|
2014-02-06 02:31:36 +00:00
|
|
|
{
|
|
|
|
if (operation === 0) { child[key[0]][key[1]][key[2]] = value; }
|
|
|
|
else if (operation == 1) { child[key[0]][key[1]][key[2]] += value; }
|
|
|
|
else if (operation == 2) { child[key[0]][key[1]][key[2]] -= value; }
|
|
|
|
else if (operation == 3) { child[key[0]][key[1]][key[2]] *= value; }
|
|
|
|
else if (operation == 4) { child[key[0]][key[1]][key[2]] /= value; }
|
|
|
|
}
|
2014-05-01 10:45:17 +00:00
|
|
|
else if (len === 4)
|
2014-02-06 02:31:36 +00:00
|
|
|
{
|
|
|
|
if (operation === 0) { child[key[0]][key[1]][key[2]][key[3]] = value; }
|
|
|
|
else if (operation == 1) { child[key[0]][key[1]][key[2]][key[3]] += value; }
|
|
|
|
else if (operation == 2) { child[key[0]][key[1]][key[2]][key[3]] -= value; }
|
|
|
|
else if (operation == 3) { child[key[0]][key[1]][key[2]][key[3]] *= value; }
|
|
|
|
else if (operation == 4) { child[key[0]][key[1]][key[2]][key[3]] /= value; }
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-05-01 10:45:17 +00:00
|
|
|
return true;
|
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* This function allows you to quickly set a property on a single child of this Group to a new value.
|
|
|
|
* The operation parameter controls how the new value is assigned to the property, from simple replacement to addition and multiplication.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#set
|
|
|
|
* @param {Phaser.Sprite} child - The child to set the property on.
|
|
|
|
* @param {string} key - The property, as a string, to be set. For example: 'body.velocity.x'
|
|
|
|
* @param {*} value - The value that will be set.
|
|
|
|
* @param {boolean} [checkAlive=false] - If set then the child will only be updated if alive=true.
|
|
|
|
* @param {boolean} [checkVisible=false] - If set then the child will only be updated if visible=true.
|
|
|
|
* @param {number} [operation=0] - Controls how the value is assigned. A value of 0 replaces the value with the new one. A value of 1 adds it, 2 subtracts it, 3 multiplies it and 4 divides it.
|
2014-05-01 10:45:17 +00:00
|
|
|
* @param {boolean} [force=false] - If `force` is true then the property will be set on the child regardless if it already exists or not. If false and the property doesn't exist, nothing will be set.
|
|
|
|
* @return {boolean} True if the property was set, false if not.
|
2014-02-06 02:31:36 +00:00
|
|
|
*/
|
2014-05-01 10:45:17 +00:00
|
|
|
Phaser.Group.prototype.set = function (child, key, value, checkAlive, checkVisible, operation, force) {
|
|
|
|
|
|
|
|
if (typeof force === 'undefined') { force = false; }
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
key = key.split('.');
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (typeof checkAlive === 'undefined') { checkAlive = false; }
|
|
|
|
if (typeof checkVisible === 'undefined') { checkVisible = false; }
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if ((checkAlive === false || (checkAlive && child.alive)) && (checkVisible === false || (checkVisible && child.visible)))
|
|
|
|
{
|
2014-05-01 10:45:17 +00:00
|
|
|
return this.setProperty(child, key, value, operation, force);
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* This function allows you to quickly set the same property across all children of this Group to a new value.
|
2014-03-19 13:09:29 +00:00
|
|
|
* This call doesn't descend down children, so if you have a Group inside of this Group, the property will be set on the Group but not its children.
|
|
|
|
* If you need that ability please see `Group.setAllChildren`.
|
|
|
|
*
|
2014-02-06 02:31:36 +00:00
|
|
|
* The operation parameter controls how the new value is assigned to the property, from simple replacement to addition and multiplication.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#setAll
|
|
|
|
* @param {string} key - The property, as a string, to be set. For example: 'body.velocity.x'
|
|
|
|
* @param {*} value - The value that will be set.
|
2014-03-19 13:09:29 +00:00
|
|
|
* @param {boolean} [checkAlive=false] - If set then only children with alive=true will be updated. This includes any Groups that are children.
|
|
|
|
* @param {boolean} [checkVisible=false] - If set then only children with visible=true will be updated. This includes any Groups that are children.
|
2014-02-06 02:31:36 +00:00
|
|
|
* @param {number} [operation=0] - Controls how the value is assigned. A value of 0 replaces the value with the new one. A value of 1 adds it, 2 subtracts it, 3 multiplies it and 4 divides it.
|
2014-05-01 10:45:17 +00:00
|
|
|
* @param {boolean} [force=false] - If `force` is true then the property will be set on the child regardless if it already exists or not. If false and the property doesn't exist, nothing will be set.
|
2014-02-06 02:31:36 +00:00
|
|
|
*/
|
2014-05-01 10:45:17 +00:00
|
|
|
Phaser.Group.prototype.setAll = function (key, value, checkAlive, checkVisible, operation, force) {
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (typeof checkAlive === 'undefined') { checkAlive = false; }
|
|
|
|
if (typeof checkVisible === 'undefined') { checkVisible = false; }
|
2014-05-01 10:45:17 +00:00
|
|
|
if (typeof force === 'undefined') { force = false; }
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-05-01 10:45:17 +00:00
|
|
|
key = key.split('.');
|
2014-02-06 02:31:36 +00:00
|
|
|
operation = operation || 0;
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
for (var i = 0, len = this.children.length; i < len; i++)
|
|
|
|
{
|
|
|
|
if ((!checkAlive || (checkAlive && this.children[i].alive)) && (!checkVisible || (checkVisible && this.children[i].visible)))
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-05-01 10:45:17 +00:00
|
|
|
this.setProperty(this.children[i], key, value, operation, force);
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2013-09-06 14:00:05 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-03-19 13:09:29 +00:00
|
|
|
/**
|
|
|
|
* This function allows you to quickly set the same property across all children of this Group, and any child Groups, to a new value.
|
|
|
|
*
|
|
|
|
* If this Group contains other Groups then the same property is set across their children as well, iterating down until it reaches the bottom.
|
|
|
|
* Unlike with Group.setAll the property is NOT set on child Groups itself.
|
|
|
|
*
|
|
|
|
* The operation parameter controls how the new value is assigned to the property, from simple replacement to addition and multiplication.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#setAllChildren
|
|
|
|
* @param {string} key - The property, as a string, to be set. For example: 'body.velocity.x'
|
|
|
|
* @param {*} value - The value that will be set.
|
|
|
|
* @param {boolean} [checkAlive=false] - If set then only children with alive=true will be updated. This includes any Groups that are children.
|
|
|
|
* @param {boolean} [checkVisible=false] - If set then only children with visible=true will be updated. This includes any Groups that are children.
|
|
|
|
* @param {number} [operation=0] - Controls how the value is assigned. A value of 0 replaces the value with the new one. A value of 1 adds it, 2 subtracts it, 3 multiplies it and 4 divides it.
|
2014-05-01 10:45:17 +00:00
|
|
|
* @param {boolean} [force=false] - If `force` is true then the property will be set on the child regardless if it already exists or not. If false and the property doesn't exist, nothing will be set.
|
2014-03-19 13:09:29 +00:00
|
|
|
*/
|
2014-05-01 10:45:17 +00:00
|
|
|
Phaser.Group.prototype.setAllChildren = function (key, value, checkAlive, checkVisible, operation, force) {
|
2014-03-19 13:09:29 +00:00
|
|
|
|
|
|
|
if (typeof checkAlive === 'undefined') { checkAlive = false; }
|
|
|
|
if (typeof checkVisible === 'undefined') { checkVisible = false; }
|
2014-05-01 10:45:17 +00:00
|
|
|
if (typeof force === 'undefined') { force = false; }
|
2014-03-19 13:09:29 +00:00
|
|
|
|
|
|
|
operation = operation || 0;
|
|
|
|
|
|
|
|
for (var i = 0, len = this.children.length; i < len; i++)
|
|
|
|
{
|
|
|
|
if ((!checkAlive || (checkAlive && this.children[i].alive)) && (!checkVisible || (checkVisible && this.children[i].visible)))
|
|
|
|
{
|
|
|
|
if (this.children[i] instanceof Phaser.Group)
|
|
|
|
{
|
2014-05-01 10:45:17 +00:00
|
|
|
this.children[i].setAllChildren(key, value, checkAlive, checkVisible, operation, force);
|
2014-03-19 13:09:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-01 10:45:17 +00:00
|
|
|
this.setProperty(this.children[i], key.split('.'), value, operation, force);
|
2014-03-19 13:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2014-03-19 13:09:29 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Adds the amount to the given property on all children in this Group.
|
|
|
|
* Group.addAll('x', 10) will add 10 to the child.x value.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#addAll
|
|
|
|
* @param {string} property - The property to increment, for example 'body.velocity.x' or 'angle'.
|
|
|
|
* @param {number} amount - The amount to increment the property by. If child.x = 10 then addAll('x', 40) would make child.x = 50.
|
|
|
|
* @param {boolean} checkAlive - If true the property will only be changed if the child is alive.
|
|
|
|
* @param {boolean} checkVisible - If true the property will only be changed if the child is visible.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.addAll = function (property, amount, checkAlive, checkVisible) {
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
this.setAll(property, amount, checkAlive, checkVisible, 1);
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Subtracts the amount from the given property on all children in this Group.
|
|
|
|
* Group.subAll('x', 10) will minus 10 from the child.x value.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#subAll
|
|
|
|
* @param {string} property - The property to decrement, for example 'body.velocity.x' or 'angle'.
|
|
|
|
* @param {number} amount - The amount to subtract from the property. If child.x = 50 then subAll('x', 40) would make child.x = 10.
|
|
|
|
* @param {boolean} checkAlive - If true the property will only be changed if the child is alive.
|
|
|
|
* @param {boolean} checkVisible - If true the property will only be changed if the child is visible.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.subAll = function (property, amount, checkAlive, checkVisible) {
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
this.setAll(property, amount, checkAlive, checkVisible, 2);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Multiplies the given property by the amount on all children in this Group.
|
|
|
|
* Group.multiplyAll('x', 2) will x2 the child.x value.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#multiplyAll
|
|
|
|
* @param {string} property - The property to multiply, for example 'body.velocity.x' or 'angle'.
|
|
|
|
* @param {number} amount - The amount to multiply the property by. If child.x = 10 then multiplyAll('x', 2) would make child.x = 20.
|
|
|
|
* @param {boolean} checkAlive - If true the property will only be changed if the child is alive.
|
|
|
|
* @param {boolean} checkVisible - If true the property will only be changed if the child is visible.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.multiplyAll = function (property, amount, checkAlive, checkVisible) {
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
this.setAll(property, amount, checkAlive, checkVisible, 3);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Divides the given property by the amount on all children in this Group.
|
|
|
|
* Group.divideAll('x', 2) will half the child.x value.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#divideAll
|
|
|
|
* @param {string} property - The property to divide, for example 'body.velocity.x' or 'angle'.
|
|
|
|
* @param {number} amount - The amount to divide the property by. If child.x = 100 then divideAll('x', 2) would make child.x = 50.
|
|
|
|
* @param {boolean} checkAlive - If true the property will only be changed if the child is alive.
|
|
|
|
* @param {boolean} checkVisible - If true the property will only be changed if the child is visible.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.divideAll = function (property, amount, checkAlive, checkVisible) {
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
this.setAll(property, amount, checkAlive, checkVisible, 4);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2014-02-06 02:31:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls a function on all of the children that have exists=true in this Group.
|
|
|
|
* After the existsValue parameter you can add as many parameters as you like, which will all be passed to the child callback.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2014-02-06 02:31:36 +00:00
|
|
|
* @method Phaser.Group#callAllExists
|
|
|
|
* @param {function} callback - The function that exists on the children that will be called.
|
|
|
|
* @param {boolean} existsValue - Only children with exists=existsValue will be called.
|
|
|
|
* @param {...*} parameter - Additional parameters that will be passed to the callback.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.callAllExists = function (callback, existsValue) {
|
|
|
|
|
|
|
|
var args = Array.prototype.splice.call(arguments, 2);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
for (var i = 0, len = this.children.length; i < len; i++)
|
|
|
|
{
|
|
|
|
if (this.children[i].exists === existsValue && this.children[i][callback])
|
|
|
|
{
|
|
|
|
this.children[i][callback].apply(this.children[i], args);
|
|
|
|
}
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Returns a reference to a function that exists on a child of the Group based on the given callback array.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2014-02-06 02:31:36 +00:00
|
|
|
* @method Phaser.Group#callbackFromArray
|
|
|
|
* @param {object} child - The object to inspect.
|
|
|
|
* @param {array} callback - The array of function names.
|
|
|
|
* @param {number} length - The size of the array (pre-calculated in callAll).
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.callbackFromArray = function (child, callback, length) {
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
// Kinda looks like a Christmas tree
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (length == 1)
|
|
|
|
{
|
|
|
|
if (child[callback[0]])
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-02-06 02:31:36 +00:00
|
|
|
return child[callback[0]];
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
|
|
|
else if (length == 2)
|
|
|
|
{
|
|
|
|
if (child[callback[0]][callback[1]])
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-02-06 02:31:36 +00:00
|
|
|
return child[callback[0]][callback[1]];
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
|
|
|
else if (length == 3)
|
|
|
|
{
|
|
|
|
if (child[callback[0]][callback[1]][callback[2]])
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-02-06 02:31:36 +00:00
|
|
|
return child[callback[0]][callback[1]][callback[2]];
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
|
|
|
else if (length == 4)
|
|
|
|
{
|
|
|
|
if (child[callback[0]][callback[1]][callback[2]][callback[3]])
|
2013-12-22 04:27:12 +00:00
|
|
|
{
|
2014-02-06 02:31:36 +00:00
|
|
|
return child[callback[0]][callback[1]][callback[2]][callback[3]];
|
2013-12-22 04:27:12 +00:00
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (child[callback])
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-02-06 02:31:36 +00:00
|
|
|
return child[callback];
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2013-09-06 14:00:05 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
return false;
|
2013-09-20 12:55:33 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-09-20 12:55:33 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Calls a function on all of the children regardless if they are dead or alive (see callAllExists if you need control over that)
|
|
|
|
* After the method parameter and context you can add as many extra parameters as you like, which will all be passed to the child.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2014-02-06 02:31:36 +00:00
|
|
|
* @method Phaser.Group#callAll
|
|
|
|
* @param {string} method - A string containing the name of the function that will be called. The function must exist on the child.
|
|
|
|
* @param {string} [context=null] - A string containing the context under which the method will be executed. Set to null to default to the child.
|
|
|
|
* @param {...*} parameter - Additional parameters that will be passed to the method.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.callAll = function (method, context) {
|
2013-09-20 12:55:33 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (typeof method === 'undefined')
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-20 12:55:33 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
// Extract the method into an array
|
|
|
|
method = method.split('.');
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
var methodLength = method.length;
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-25 05:20:19 +00:00
|
|
|
if (typeof context === 'undefined' || context === null || context === '')
|
2014-02-06 02:31:36 +00:00
|
|
|
{
|
|
|
|
context = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Extract the context into an array
|
|
|
|
if (typeof context === 'string')
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-02-06 02:31:36 +00:00
|
|
|
context = context.split('.');
|
|
|
|
var contextLength = context.length;
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
var args = Array.prototype.splice.call(arguments, 2);
|
|
|
|
var callback = null;
|
|
|
|
var callbackContext = null;
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
for (var i = 0, len = this.children.length; i < len; i++)
|
|
|
|
{
|
|
|
|
callback = this.callbackFromArray(this.children[i], method, methodLength);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (context && callback)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-02-06 02:31:36 +00:00
|
|
|
callbackContext = this.callbackFromArray(this.children[i], context, contextLength);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (callback)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-02-06 02:31:36 +00:00
|
|
|
callback.apply(callbackContext, args);
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
else if (callback)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-02-06 02:31:36 +00:00
|
|
|
callback.apply(this.children[i], args);
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-13 12:26:39 +00:00
|
|
|
/**
|
|
|
|
* The core preUpdate - as called by World.
|
|
|
|
* @method Phaser.Group#preUpdate
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.preUpdate = function () {
|
|
|
|
|
2014-02-15 01:27:42 +00:00
|
|
|
if (!this.exists || !this.parent.exists)
|
|
|
|
{
|
|
|
|
this.renderOrderID = -1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-13 16:49:52 +00:00
|
|
|
var i = this.children.length;
|
2014-02-15 02:19:37 +00:00
|
|
|
|
|
|
|
while (i--)
|
2014-02-13 12:26:39 +00:00
|
|
|
{
|
2014-02-14 01:09:52 +00:00
|
|
|
this.children[i].preUpdate();
|
2014-02-13 12:26:39 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 01:27:42 +00:00
|
|
|
return true;
|
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2014-02-13 12:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The core update - as called by World.
|
|
|
|
* @method Phaser.Group#update
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.update = function () {
|
|
|
|
|
2014-02-15 02:19:37 +00:00
|
|
|
var i = this.children.length;
|
|
|
|
|
|
|
|
while (i--)
|
2014-02-13 12:26:39 +00:00
|
|
|
{
|
2014-02-14 01:09:52 +00:00
|
|
|
this.children[i].update();
|
2014-02-13 12:26:39 +00:00
|
|
|
}
|
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2014-02-13 12:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The core postUpdate - as called by World.
|
|
|
|
* @method Phaser.Group#postUpdate
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.postUpdate = function () {
|
|
|
|
|
2014-02-15 01:27:42 +00:00
|
|
|
// Fixed to Camera?
|
|
|
|
if (this._cache[7] === 1)
|
|
|
|
{
|
|
|
|
this.x = this.game.camera.view.x + this.cameraOffset.x;
|
|
|
|
this.y = this.game.camera.view.y + this.cameraOffset.y;
|
|
|
|
}
|
|
|
|
|
2014-02-15 02:19:37 +00:00
|
|
|
var i = this.children.length;
|
|
|
|
|
|
|
|
while (i--)
|
2014-02-13 12:26:39 +00:00
|
|
|
{
|
2014-02-14 01:09:52 +00:00
|
|
|
this.children[i].postUpdate();
|
2014-02-13 12:26:39 +00:00
|
|
|
}
|
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2014-02-13 12:26:39 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Allows you to call your own function on each member of this Group. You must pass the callback and context in which it will run.
|
|
|
|
* After the checkExists parameter you can add as many parameters as you like, which will all be passed to the callback along with the child.
|
|
|
|
* For example: Group.forEach(awardBonusGold, this, true, 100, 500)
|
|
|
|
* Note: Currently this will skip any children which are Groups themselves.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2014-02-06 02:31:36 +00:00
|
|
|
* @method Phaser.Group#forEach
|
|
|
|
* @param {function} callback - The function that will be called. Each child of the Group will be passed to it as its first parameter.
|
|
|
|
* @param {Object} callbackContext - The context in which the function should be called (usually 'this').
|
2014-03-26 13:24:11 +00:00
|
|
|
* @param {boolean} [checkExists=false] - If set only children with exists=true will be passed to the callback, otherwise all children will be passed.
|
2014-02-06 02:31:36 +00:00
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.forEach = function (callback, callbackContext, checkExists) {
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-26 13:24:11 +00:00
|
|
|
if (typeof checkExists === 'undefined') { checkExists = false; }
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
var args = Array.prototype.splice.call(arguments, 3);
|
|
|
|
args.unshift(null);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
for (var i = 0, len = this.children.length; i < len; i++)
|
|
|
|
{
|
|
|
|
if (!checkExists || (checkExists && this.children[i].exists))
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-02-06 02:31:36 +00:00
|
|
|
args[0] = this.children[i];
|
|
|
|
callback.apply(callbackContext, args);
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
2014-02-19 15:06:22 +00:00
|
|
|
* Allows you to call your own function on each member of this Group where child.exists=true. You must pass the callback and context in which it will run.
|
2014-02-06 02:31:36 +00:00
|
|
|
* You can add as many parameters as you like, which will all be passed to the callback along with the child.
|
2014-02-19 15:06:22 +00:00
|
|
|
* For example: Group.forEachExists(causeDamage, this, 500)
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2014-02-19 15:06:22 +00:00
|
|
|
* @method Phaser.Group#forEachExists
|
2014-02-06 02:31:36 +00:00
|
|
|
* @param {function} callback - The function that will be called. Each child of the Group will be passed to it as its first parameter.
|
|
|
|
* @param {Object} callbackContext - The context in which the function should be called (usually 'this').
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.forEachExists = function (callback, callbackContext) {
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
var args = Array.prototype.splice.call(arguments, 2);
|
|
|
|
args.unshift(null);
|
2013-10-01 01:19:08 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
this.iterate('exists', true, Phaser.Group.RETURN_TOTAL, callback, callbackContext, args);
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Allows you to call your own function on each alive member of this Group (where child.alive=true). You must pass the callback and context in which it will run.
|
|
|
|
* You can add as many parameters as you like, which will all be passed to the callback along with the child.
|
|
|
|
* For example: Group.forEachAlive(causeDamage, this, 500)
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2014-02-06 02:31:36 +00:00
|
|
|
* @method Phaser.Group#forEachAlive
|
|
|
|
* @param {function} callback - The function that will be called. Each child of the Group will be passed to it as its first parameter.
|
|
|
|
* @param {Object} callbackContext - The context in which the function should be called (usually 'this').
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.forEachAlive = function (callback, callbackContext) {
|
2013-11-06 16:46:21 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
var args = Array.prototype.splice.call(arguments, 2);
|
|
|
|
args.unshift(null);
|
2013-11-06 16:46:21 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
this.iterate('alive', true, Phaser.Group.RETURN_TOTAL, callback, callbackContext, args);
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Allows you to call your own function on each dead member of this Group (where alive=false). You must pass the callback and context in which it will run.
|
|
|
|
* You can add as many parameters as you like, which will all be passed to the callback along with the child.
|
|
|
|
* For example: Group.forEachDead(bringToLife, this)
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2014-02-06 02:31:36 +00:00
|
|
|
* @method Phaser.Group#forEachDead
|
|
|
|
* @param {function} callback - The function that will be called. Each child of the Group will be passed to it as its first parameter.
|
|
|
|
* @param {Object} callbackContext - The context in which the function should be called (usually 'this').
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.forEachDead = function (callback, callbackContext) {
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
var args = Array.prototype.splice.call(arguments, 2);
|
|
|
|
args.unshift(null);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
this.iterate('alive', false, Phaser.Group.RETURN_TOTAL, callback, callbackContext, args);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Call this function to sort the group according to a particular value and order.
|
|
|
|
* 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()`.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#sort
|
2014-03-10 23:01:10 +00:00
|
|
|
* @param {string} [index='z'] - The `string` name of the property you want to sort on. Defaults to the objects z-depth value.
|
2014-02-06 02:31:36 +00:00
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.sort = function (index, order) {
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-10 22:14:52 +00:00
|
|
|
if (this.children.length < 2)
|
|
|
|
{
|
|
|
|
// Nothing to swap
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-14 04:21:56 +00:00
|
|
|
if (typeof index === 'undefined') { index = 'z'; }
|
2014-02-06 02:31:36 +00:00
|
|
|
if (typeof order === 'undefined') { order = Phaser.Group.SORT_ASCENDING; }
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-10 14:33:18 +00:00
|
|
|
this._sortProperty = index;
|
|
|
|
|
2014-03-10 19:06:03 +00:00
|
|
|
if (order === Phaser.Group.SORT_ASCENDING)
|
|
|
|
{
|
|
|
|
this.children.sort(this.ascendingSortHandler.bind(this));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.children.sort(this.descendingSortHandler.bind(this));
|
|
|
|
}
|
|
|
|
|
2014-03-10 23:16:49 +00:00
|
|
|
this.updateZ();
|
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2014-03-10 19:06:03 +00:00
|
|
|
|
2014-04-09 23:56:53 +00:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-03-10 19:06:03 +00:00
|
|
|
/**
|
|
|
|
* An internal helper function for the sort process.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#ascendingSortHandler
|
|
|
|
* @param {object} a - The first object being sorted.
|
|
|
|
* @param {object} b - The second object being sorted.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.ascendingSortHandler = function (a, b) {
|
|
|
|
|
2014-03-10 23:01:10 +00:00
|
|
|
if (a[this._sortProperty] < b[this._sortProperty])
|
2014-03-10 19:06:03 +00:00
|
|
|
{
|
2014-03-10 22:14:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-10 23:01:10 +00:00
|
|
|
else if (a[this._sortProperty] > b[this._sortProperty])
|
2014-03-10 22:14:52 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-10 23:01:10 +00:00
|
|
|
if (a.z < b.z)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2014-03-10 19:06:03 +00:00
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-10 14:33:18 +00:00
|
|
|
/**
|
|
|
|
* An internal helper function for the sort process.
|
|
|
|
*
|
2014-03-10 19:06:03 +00:00
|
|
|
* @method Phaser.Group#descendingSortHandler
|
2014-03-10 14:33:18 +00:00
|
|
|
* @param {object} a - The first object being sorted.
|
|
|
|
* @param {object} b - The second object being sorted.
|
|
|
|
*/
|
2014-03-10 19:06:03 +00:00
|
|
|
Phaser.Group.prototype.descendingSortHandler = function (a, b) {
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-10 23:01:10 +00:00
|
|
|
if (a[this._sortProperty] < b[this._sortProperty])
|
2014-03-10 14:33:18 +00:00
|
|
|
{
|
2014-03-10 23:01:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (a[this._sortProperty] > b[this._sortProperty])
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
2014-03-10 14:33:18 +00:00
|
|
|
}
|
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* You can add as many callback parameters as you like, which will all be passed to the callback along with the child, after the callbackContext parameter.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2014-02-06 02:31:36 +00:00
|
|
|
* @method Phaser.Group#iterate
|
|
|
|
* @param {string} key - The child property to check, i.e. 'exists', 'alive', 'health'
|
|
|
|
* @param {any} value - If child.key === this value it will be considered a match. Note that a strict comparison is used.
|
|
|
|
* @param {number} returnType - How to return the data from this method. Either Phaser.Group.RETURN_NONE, Phaser.Group.RETURN_TOTAL or Phaser.Group.RETURN_CHILD.
|
|
|
|
* @param {function} [callback=null] - Optional function that will be called on each matching child. Each child of the Group will be passed to it as its first parameter.
|
|
|
|
* @param {Object} [callbackContext] - The context in which the function should be called (usually 'this').
|
|
|
|
* @return {any} Returns either a numeric total (if RETURN_TOTAL was specified) or the child object.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.iterate = function (key, value, returnType, callback, callbackContext, args) {
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (returnType === Phaser.Group.RETURN_TOTAL && this.children.length === 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (typeof callback === 'undefined')
|
|
|
|
{
|
|
|
|
callback = false;
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
var total = 0;
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
for (var i = 0, len = this.children.length; i < len; i++)
|
|
|
|
{
|
|
|
|
if (this.children[i][key] === value)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-02-06 02:31:36 +00:00
|
|
|
total++;
|
|
|
|
|
|
|
|
if (callback)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-02-06 02:31:36 +00:00
|
|
|
args[0] = this.children[i];
|
|
|
|
callback.apply(callbackContext, args);
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (returnType === Phaser.Group.RETURN_CHILD)
|
|
|
|
{
|
|
|
|
return this.children[i];
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (returnType === Phaser.Group.RETURN_TOTAL)
|
|
|
|
{
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
else if (returnType === Phaser.Group.RETURN_CHILD)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Call this function to retrieve the first object with exists == (the given state) in the Group.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#getFirstExists
|
|
|
|
* @param {boolean} state - True or false.
|
|
|
|
* @return {Any} The first child, or null if none found.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.getFirstExists = function (state) {
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (typeof state !== 'boolean')
|
|
|
|
{
|
|
|
|
state = true;
|
|
|
|
}
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
return this.iterate('exists', state, Phaser.Group.RETURN_CHILD);
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Call this function to retrieve the first object with alive === true in the group.
|
|
|
|
* This is handy for checking if everything has been wiped out, or choosing a squad leader, etc.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#getFirstAlive
|
|
|
|
* @return {Any} The first alive child, or null if none found.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.getFirstAlive = function () {
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
return this.iterate('alive', true, Phaser.Group.RETURN_CHILD);
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Call this function to retrieve the first object with alive === false in the group.
|
|
|
|
* This is handy for checking if everything has been wiped out, or choosing a squad leader, etc.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#getFirstDead
|
|
|
|
* @return {Any} The first dead child, or null if none found.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.getFirstDead = function () {
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
return this.iterate('alive', false, Phaser.Group.RETURN_CHILD);
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-03-10 23:01:10 +00:00
|
|
|
/**
|
|
|
|
* Returns the child at the top of this Group. The top is the one being displayed (rendered) above every other child.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#getTop
|
|
|
|
* @return {Any} The child at the top of the Group.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.getTop = function () {
|
|
|
|
|
|
|
|
if (this.children.length > 0)
|
|
|
|
{
|
|
|
|
return this.children[this.children.length - 1];
|
|
|
|
}
|
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2014-03-10 23:01:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the child at the bottom of this Group. The bottom is the one being displayed (rendered) below every other child.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#getBottom
|
|
|
|
* @return {Any} The child at the bottom of the Group.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.getBottom = function () {
|
|
|
|
|
|
|
|
if (this.children.length > 0)
|
|
|
|
{
|
|
|
|
return this.children[0];
|
|
|
|
}
|
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2014-03-10 23:01:10 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Call this function to find out how many members of the group are alive.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#countLiving
|
|
|
|
* @return {number} The number of children flagged as alive.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.countLiving = function () {
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
return this.iterate('alive', true, Phaser.Group.RETURN_TOTAL);
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Call this function to find out how many members of the group are dead.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#countDead
|
|
|
|
* @return {number} The number of children flagged as dead.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.countDead = function () {
|
2013-09-06 14:00:05 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
return this.iterate('alive', false, Phaser.Group.RETURN_TOTAL);
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Returns a member at random from the group.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#getRandom
|
|
|
|
* @param {number} startIndex - Optional offset off the front of the array. Default value is 0, or the beginning of the array.
|
|
|
|
* @param {number} length - Optional restriction on the number of values you want to randomly select from.
|
|
|
|
* @return {Any} A random child of this Group.
|
|
|
|
*/
|
|
|
|
Phaser.Group.prototype.getRandom = function (startIndex, length) {
|
2013-09-06 14:00:05 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (this.children.length === 0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2013-09-14 23:14:45 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
startIndex = startIndex || 0;
|
|
|
|
length = length || this.children.length;
|
2013-09-06 14:00:05 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
return this.game.math.getRandom(this.children, startIndex, length);
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
2014-04-17 11:42:19 +00:00
|
|
|
* Removes the given child from this Group. This will dispatch an onRemovedFromGroup event from the child (if it has one),
|
|
|
|
* reset the Group cursor and optionally destroy the child.
|
2014-02-06 02:31:36 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Group#remove
|
|
|
|
* @param {Any} child - The child to remove.
|
2014-04-10 23:06:22 +00:00
|
|
|
* @param {boolean} [destroy=false] - You can optionally call destroy on the child that was removed.
|
2014-07-02 13:04:14 +00:00
|
|
|
* @param {boolean} [silent=false] - If the silent parameter is `true` the child will not dispatch the onRemovedFromGroup event.
|
2014-02-06 02:31:36 +00:00
|
|
|
* @return {boolean} true if the child was removed from this Group, otherwise false.
|
|
|
|
*/
|
2014-07-02 13:04:14 +00:00
|
|
|
Phaser.Group.prototype.remove = function (child, destroy, silent) {
|
2014-04-10 23:06:22 +00:00
|
|
|
|
|
|
|
if (typeof destroy === 'undefined') { destroy = false; }
|
2014-07-02 13:04:14 +00:00
|
|
|
if (typeof silent === 'undefined') { silent = false; }
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-04-17 11:42:19 +00:00
|
|
|
if (this.children.length === 0 || this.children.indexOf(child) === -1)
|
2014-02-06 02:31:36 +00:00
|
|
|
{
|
2014-04-10 23:06:22 +00:00
|
|
|
return false;
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2013-11-26 05:13:56 +00:00
|
|
|
|
2014-07-02 13:04:14 +00:00
|
|
|
if (!silent && child.events && !child.destroyPhase)
|
2014-02-06 02:31:36 +00:00
|
|
|
{
|
|
|
|
child.events.onRemovedFromGroup.dispatch(child, this);
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-04-14 11:57:29 +00:00
|
|
|
var removed = this.removeChild(child);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-10 23:01:10 +00:00
|
|
|
this.updateZ();
|
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (this.cursor === child)
|
|
|
|
{
|
|
|
|
this.next();
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-04-14 11:57:29 +00:00
|
|
|
if (destroy && removed)
|
2014-04-10 23:06:22 +00:00
|
|
|
{
|
2014-04-16 17:46:25 +00:00
|
|
|
removed.destroy(true);
|
2014-04-10 23:06:22 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
return true;
|
2013-11-26 05:13:56 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
2014-07-02 13:04:14 +00:00
|
|
|
* Removes all children from this Group, setting the group properties of the children to `null`.
|
2014-02-06 02:31:36 +00:00
|
|
|
* The Group container remains on the display list.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#removeAll
|
2014-07-02 13:04:14 +00:00
|
|
|
* @param {boolean} [destroy=false] - You can optionally call destroy on each child that is removed.
|
|
|
|
* @param {boolean} [silent=false] - If the silent parameter is `true` the children will not dispatch their onRemovedFromGroup events.
|
2014-02-06 02:31:36 +00:00
|
|
|
*/
|
2014-07-02 13:04:14 +00:00
|
|
|
Phaser.Group.prototype.removeAll = function (destroy, silent) {
|
2014-04-10 23:06:22 +00:00
|
|
|
|
|
|
|
if (typeof destroy === 'undefined') { destroy = false; }
|
2014-07-02 13:04:14 +00:00
|
|
|
if (typeof silent === 'undefined') { silent = false; }
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (this.children.length === 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
do
|
|
|
|
{
|
2014-07-02 13:04:14 +00:00
|
|
|
if (!silent && this.children[0].events)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-02-06 02:31:36 +00:00
|
|
|
this.children[0].events.onRemovedFromGroup.dispatch(this.children[0], this);
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2014-02-06 13:15:45 +00:00
|
|
|
|
2014-04-14 11:57:29 +00:00
|
|
|
var removed = this.removeChild(this.children[0]);
|
2014-04-10 23:06:22 +00:00
|
|
|
|
2014-04-14 11:57:29 +00:00
|
|
|
if (destroy && removed)
|
2014-04-10 23:06:22 +00:00
|
|
|
{
|
2014-04-16 17:46:25 +00:00
|
|
|
removed.destroy(true);
|
2014-04-10 23:06:22 +00:00
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
|
|
|
while (this.children.length > 0);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
this.cursor = null;
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Removes all children from this Group whos index falls beteen the given startIndex and endIndex values.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#removeBetween
|
|
|
|
* @param {number} startIndex - The index to start removing children from.
|
2014-04-09 02:19:36 +00:00
|
|
|
* @param {number} [endIndex] - The index to stop removing children at. Must be higher than startIndex. If undefined this method will remove all children between startIndex and the end of the Group.
|
2014-04-10 23:06:22 +00:00
|
|
|
* @param {boolean} [destroy=false] - You can optionally call destroy on the child that was removed.
|
2014-07-02 13:04:14 +00:00
|
|
|
* @param {boolean} [silent=false] - If the silent parameter is `true` the children will not dispatch their onRemovedFromGroup events.
|
2014-02-06 02:31:36 +00:00
|
|
|
*/
|
2014-07-02 13:04:14 +00:00
|
|
|
Phaser.Group.prototype.removeBetween = function (startIndex, endIndex, destroy, silent) {
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-04-09 02:19:36 +00:00
|
|
|
if (typeof endIndex === 'undefined') { endIndex = this.children.length; }
|
2014-04-10 23:06:22 +00:00
|
|
|
if (typeof destroy === 'undefined') { destroy = false; }
|
2014-07-02 13:04:14 +00:00
|
|
|
if (typeof silent === 'undefined') { silent = false; }
|
2014-04-09 02:19:36 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (this.children.length === 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
if (startIndex > endIndex || startIndex < 0 || endIndex > this.children.length)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-04-09 02:19:36 +00:00
|
|
|
var i = endIndex;
|
|
|
|
|
|
|
|
while (i >= startIndex)
|
2014-02-06 02:31:36 +00:00
|
|
|
{
|
2014-07-02 13:04:14 +00:00
|
|
|
if (!silent && this.children[i].events)
|
2014-02-06 13:15:45 +00:00
|
|
|
{
|
|
|
|
this.children[i].events.onRemovedFromGroup.dispatch(this.children[i], this);
|
|
|
|
}
|
|
|
|
|
2014-04-14 11:57:29 +00:00
|
|
|
var removed = this.removeChild(this.children[i]);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-04-14 11:57:29 +00:00
|
|
|
if (destroy && removed)
|
2014-04-10 23:06:22 +00:00
|
|
|
{
|
2014-04-16 17:46:25 +00:00
|
|
|
removed.destroy(true);
|
2014-04-10 23:06:22 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 16:49:52 +00:00
|
|
|
if (this.cursor === this.children[i])
|
2013-12-30 21:36:50 +00:00
|
|
|
{
|
2014-02-06 02:31:36 +00:00
|
|
|
this.cursor = null;
|
2013-12-30 21:36:50 +00:00
|
|
|
}
|
2014-04-09 02:19:36 +00:00
|
|
|
|
|
|
|
i--;
|
2014-02-06 02:31:36 +00:00
|
|
|
}
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-10 23:01:10 +00:00
|
|
|
this.updateZ();
|
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
/**
|
|
|
|
* Destroys this Group. Removes all children, then removes the container from the display list and nulls references.
|
|
|
|
*
|
|
|
|
* @method Phaser.Group#destroy
|
2014-02-27 20:05:16 +00:00
|
|
|
* @param {boolean} [destroyChildren=true] - Should every child of this Group have its destroy method called?
|
2014-03-19 00:54:49 +00:00
|
|
|
* @param {boolean} [soft=false] - A 'soft destroy' (set to true) doesn't remove this Group from its parent or null the game reference. Set to false and it does.
|
2014-02-06 02:31:36 +00:00
|
|
|
*/
|
2014-03-19 00:54:49 +00:00
|
|
|
Phaser.Group.prototype.destroy = function (destroyChildren, soft) {
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-02-27 20:05:16 +00:00
|
|
|
if (this.game === null) { return; }
|
|
|
|
|
|
|
|
if (typeof destroyChildren === 'undefined') { destroyChildren = true; }
|
2014-03-19 00:54:49 +00:00
|
|
|
if (typeof soft === 'undefined') { soft = false; }
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-04-14 11:57:29 +00:00
|
|
|
this.removeAll(destroyChildren);
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-19 00:54:49 +00:00
|
|
|
this.cursor = null;
|
2014-05-26 19:12:45 +00:00
|
|
|
this.filters = null;
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-03-19 00:54:49 +00:00
|
|
|
if (!soft)
|
|
|
|
{
|
2014-04-17 11:18:39 +00:00
|
|
|
if (this.parent)
|
|
|
|
{
|
|
|
|
this.parent.removeChild(this);
|
|
|
|
}
|
2013-09-05 20:07:44 +00:00
|
|
|
|
2014-03-19 00:54:49 +00:00
|
|
|
this.game = null;
|
|
|
|
this.exists = false;
|
|
|
|
}
|
2014-02-06 02:31:36 +00:00
|
|
|
|
2014-03-23 08:29:04 +00:00
|
|
|
};
|
2013-12-30 16:54:00 +00:00
|
|
|
|
2013-10-08 20:09:46 +00:00
|
|
|
/**
|
|
|
|
* @name Phaser.Group#total
|
2013-12-05 09:14:49 +00:00
|
|
|
* @property {number} total - The total number of children in this Group who have a state of exists = true.
|
2013-10-08 20:09:46 +00:00
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Group.prototype, "total", {
|
|
|
|
|
|
|
|
get: function () {
|
2013-12-30 21:36:50 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
return this.iterate('exists', true, Phaser.Group.RETURN_TOTAL);
|
2013-12-30 21:36:50 +00:00
|
|
|
|
2013-10-08 20:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-02 00:16:40 +00:00
|
|
|
* @name Phaser.Group#length
|
2013-12-05 09:14:49 +00:00
|
|
|
* @property {number} length - The total number of children in this Group, regardless of their exists/alive status.
|
2013-10-02 11:11:22 +00:00
|
|
|
* @readonly
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-19 03:45:08 +00:00
|
|
|
Object.defineProperty(Phaser.Group.prototype, "length", {
|
|
|
|
|
|
|
|
get: function () {
|
2013-12-30 21:36:50 +00:00
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
return this.children.length;
|
2013-12-30 21:36:50 +00:00
|
|
|
|
2013-09-19 03:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-02 00:16:40 +00:00
|
|
|
* The angle of rotation of the Group container. This will adjust the Group container itself by modifying its rotation.
|
|
|
|
* This will have no impact on the rotation value of its children, but it will update their worldTransform and on-screen position.
|
|
|
|
* @name Phaser.Group#angle
|
|
|
|
* @property {number} angle - The angle of rotation given in degrees, where 0 degrees = to the right.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-06 14:00:05 +00:00
|
|
|
Object.defineProperty(Phaser.Group.prototype, "angle", {
|
|
|
|
|
|
|
|
get: function() {
|
2014-02-06 02:31:36 +00:00
|
|
|
return Phaser.Math.radToDeg(this.rotation);
|
2013-09-06 14:00:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
2014-02-06 02:31:36 +00:00
|
|
|
this.rotation = Phaser.Math.degToRad(value);
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-09-06 14:00:05 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-02-15 01:27:42 +00:00
|
|
|
/**
|
|
|
|
* A Group that is fixed to the camera uses its x/y coordinates as offsets from the top left of the camera. These are stored in Group.cameraOffset.
|
|
|
|
* Note that the cameraOffset values are in addition to any parent in the display list.
|
|
|
|
* So if this Group was in a Group that has x: 200, then this will be added to the cameraOffset.x
|
|
|
|
*
|
|
|
|
* @name Phaser.Group#fixedToCamera
|
|
|
|
* @property {boolean} fixedToCamera - Set to true to fix this Group to the Camera at its current world coordinates.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Group.prototype, "fixedToCamera", {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-02-15 01:27:42 +00:00
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return !!this._cache[7];
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
this._cache[7] = 1;
|
|
|
|
this.cameraOffset.set(this.x, this.y);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._cache[7] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-02-06 02:31:36 +00:00
|
|
|
// Documentation stubs
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The x coordinate of the Group container. You can adjust the Group container itself by modifying its coordinates.
|
|
|
|
* This will have no impact on the x/y coordinates of its children, but it will update their worldTransform and on-screen position.
|
|
|
|
* @name Phaser.Group#x
|
|
|
|
* @property {number} x - The x coordinate of the Group container.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The y coordinate of the Group container. You can adjust the Group container itself by modifying its coordinates.
|
|
|
|
* This will have no impact on the x/y coordinates of its children, but it will update their worldTransform and on-screen position.
|
|
|
|
* @name Phaser.Group#y
|
|
|
|
* @property {number} y - The y coordinate of the Group container.
|
|
|
|
*/
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-02 00:16:40 +00:00
|
|
|
* The angle of rotation of the Group container. This will adjust the Group container itself by modifying its rotation.
|
|
|
|
* This will have no impact on the rotation value of its children, but it will update their worldTransform and on-screen position.
|
|
|
|
* @name Phaser.Group#rotation
|
|
|
|
* @property {number} rotation - The angle of rotation given in radians.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-06 14:00:05 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-02 00:16:40 +00:00
|
|
|
* @name Phaser.Group#visible
|
|
|
|
* @property {boolean} visible - The visible state of the Group. Non-visible Groups and all of their children are not rendered.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-10-13 19:28:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Group#alpha
|
|
|
|
* @property {number} alpha - The alpha value of the Group container.
|
|
|
|
*/
|