phaser-ce
Version:
Phaser CE (Community Edition) is a fast, free and fun HTML5 Game Framework for Desktop and Mobile web browsers.
1,368 lines (1,221 loc) • 111 kB
JavaScript
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* A Group is a container for {@link DisplayObject display objects} including {@link Phaser.Sprite Sprites} and {@link Phaser.Image Images}.
*
* Groups form the logical tree structure of the display/scene graph where local transformations are applied to children.
* For instance, all children are also moved/rotated/scaled when the group is moved/rotated/scaled.
*
* In addition, Groups provides support for fast pooling and object recycling.
*
* Groups are also display objects and can be nested as children within other Groups.
*
* @class Phaser.Group
* @extends PIXI.DisplayObjectContainer
* @param {Phaser.Game} game - A reference to the currently running game.
* @param {DisplayObject|null} [parent=(game world)] - The parent Group (or other {@link DisplayObject}) that this group will be added to.
* If undefined/unspecified the Group will be added to the {@link Phaser.Game#world Game World}; if null the Group will not be added to any parent.
* @param {string} [name='group'] - A name for this group. Not used internally but useful for debugging.
* @param {boolean} [addToStage=false] - If true this group will be added directly to the Game.Stage instead of Game.World.
* @param {boolean} [enableBody=false] - If true all Sprites created with {@link #create} or {@link #createMulitple} will have a physics body created on them. Change the body type with {@link #physicsBodyType}.
* @param {integer} [physicsBodyType=0] - The physics body type to use when physics bodies are automatically added. See {@link #physicsBodyType} for values.
*/
Phaser.Group = function (game, parent, name, addToStage, enableBody, physicsBodyType)
{
if (addToStage === undefined) { addToStage = false; }
if (enableBody === undefined) { enableBody = false; }
if (physicsBodyType === undefined) { physicsBodyType = Phaser.Physics.ARCADE; }
/**
* A reference to the currently running Game.
* @property {Phaser.Game} game
* @protected
*/
this.game = game;
if (parent === undefined)
{
parent = game.world;
}
/**
* A name for this group. Not used internally but useful for debugging.
* @property {string} name
*/
this.name = name || 'group';
/**
* The z-depth value of this object within its parent container/Group - the World is a Group as well.
* This value must be unique for each child in a Group.
* @property {integer} z
* @readOnly
*/
this.z = 0;
PIXI.DisplayObjectContainer.call(this);
if (addToStage)
{
this.game.stage.addChild(this);
this.z = this.game.stage.children.length;
}
else
if (parent)
{
parent.addChild(this);
this.z = parent.children.length;
}
/**
* Internal Phaser Type value.
* @property {integer} type
* @protected
*/
this.type = Phaser.GROUP;
/**
* @property {number} physicsType - The const physics body type of this object.
* @readonly
*/
this.physicsType = Phaser.GROUP;
/**
* The alive property is useful for Groups that are children of other Groups and need to be included/excluded in checks like forEachAlive.
* @property {boolean} alive
* @default
*/
this.alive = true;
/**
* If exists is false the group will be excluded from collision checks and filters such as {@link #forEachExists}. The group will not call `preUpdate` and `postUpdate` on its children and the children will not receive physics updates or camera/world boundary checks. The group will still be {@link #visible} and will still call `update` on its children (unless {@link #updateOnlyExistingChildren} is true).
* @property {boolean} exists
* @default
*/
this.exists = true;
/**
* A group with `ignoreDestroy` set to `true` ignores all calls to its `destroy` method.
* @property {boolean} ignoreDestroy
* @default
*/
this.ignoreDestroy = false;
/**
* A Group is that has `pendingDestroy` set to `true` is flagged to have its destroy method
* called on the next logic update.
* You can set it directly to flag the Group to be destroyed on its next update.
*
* This is extremely useful if you wish to destroy a Group from within one of its own callbacks
* or a callback of one of its children.
*
* @property {boolean} pendingDestroy
*/
this.pendingDestroy = false;
/**
* The type of objects that will be created when using {@link #create} or {@link #createMultiple}.
*
* It should extend either Sprite or Image and accept the same constructor arguments: `(game, x, y, key, frame)`.
*
* @property {function} classType
* @default {@link Phaser.Sprite}
*/
this.classType = Phaser.Sprite;
/**
* The current display object that the group cursor is pointing to, if any. (Can be set manually.)
*
* The cursor is a way to iterate through the children in a Group using {@link #next} and {@link #previous}.
* @property {?DisplayObject} cursor
*/
this.cursor = null;
/**
* A Group with `inputEnableChildren` set to `true` will automatically call `inputEnabled = true`
* on any children _added_ to, or _created by_, this Group.
*
* If there are children already in the Group at the time you set this property, they are not changed.
*
* @property {boolean} inputEnableChildren
* @default
*/
this.inputEnableChildren = false;
/**
* Skip children with `exists = false` in {@link #update}.
*
* @property {boolean} updateOnlyExistingChildren
* @default
*/
this.updateOnlyExistingChildren = false;
/**
* This Signal is dispatched whenever a child of this Group emits an onInputDown signal as a result
* of having been interacted with by a Pointer. You can bind functions to this Signal instead of to
* every child Sprite.
*
* This Signal is sent 2 arguments: A reference to the Sprite that triggered the signal, and
* a reference to the Pointer that caused it.
*
* @property {Phaser.Signal} onChildInputDown
*/
this.onChildInputDown = new Phaser.Signal();
/**
* This Signal is dispatched whenever a child of this Group emits an onInputUp signal as a result
* of having been interacted with by a Pointer. You can bind functions to this Signal instead of to
* every child Sprite.
*
* This Signal is sent 3 arguments: A reference to the Sprite that triggered the signal,
* a reference to the Pointer that caused it, and a boolean value `isOver` that tells you if the Pointer
* is still over the Sprite or not.
*
* @property {Phaser.Signal} onChildInputUp
*/
this.onChildInputUp = new Phaser.Signal();
/**
* This Signal is dispatched whenever a child of this Group emits an onInputOver signal as a result
* of having been interacted with by a Pointer. You can bind functions to this Signal instead of to
* every child Sprite.
*
* This Signal is sent 2 arguments: A reference to the Sprite that triggered the signal, and
* a reference to the Pointer that caused it.
*
* @property {Phaser.Signal} onChildInputOver
*/
this.onChildInputOver = new Phaser.Signal();
/**
* This Signal is dispatched whenever a child of this Group emits an onInputOut signal as a result
* of having been interacted with by a Pointer. You can bind functions to this Signal instead of to
* every child Sprite.
*
* This Signal is sent 2 arguments: A reference to the Sprite that triggered the signal, and
* a reference to the Pointer that caused it.
*
* @property {Phaser.Signal} onChildInputOut
*/
this.onChildInputOut = new Phaser.Signal();
/**
* If true all Sprites created by, or added to this group, will have a physics body enabled on them.
*
* If there are children already in the Group at the time you set this property, they are not changed.
*
* The default body type is controlled with {@link #physicsBodyType}.
* @property {boolean} enableBody
*/
this.enableBody = enableBody;
/**
* If true when a physics body is created (via {@link #enableBody}) it will create a physics debug object as well.
*
* This only works for P2 bodies.
* @property {boolean} enableBodyDebug
* @default
*/
this.enableBodyDebug = false;
/**
* If {@link #enableBody} is true this is the type of physics body that is created on new Sprites.
*
* The valid values are {@link Phaser.Physics.ARCADE}, {@link Phaser.Physics.P2JS}, {@link Phaser.Physics.NINJA}, etc.
* @property {integer} physicsBodyType
*/
this.physicsBodyType = physicsBodyType;
/**
* If this Group contains Arcade Physics Sprites you can set a custom sort direction via this property.
*
* It should be set to one of the Phaser.Physics.Arcade sort direction constants:
*
* Phaser.Physics.Arcade.SORT_NONE
* Phaser.Physics.Arcade.LEFT_RIGHT
* Phaser.Physics.Arcade.RIGHT_LEFT
* Phaser.Physics.Arcade.TOP_BOTTOM
* Phaser.Physics.Arcade.BOTTOM_TOP
*
* If set to `null` the Group will use whatever Phaser.Physics.Arcade.sortDirection is set to. This is the default behavior.
*
* @property {integer} physicsSortDirection
* @default
*/
this.physicsSortDirection = null;
/**
* This signal is dispatched when the group is destroyed.
* @property {Phaser.Signal} onDestroy
*/
this.onDestroy = new Phaser.Signal();
/**
* @property {integer} cursorIndex - The current index of the Group cursor. Advance it with Group.next.
* @readOnly
*/
this.cursorIndex = 0;
/**
* 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
*
* @property {boolean} fixedToCamera
*/
this.fixedToCamera = false;
/**
* If this object is {@link #fixedToCamera} then this stores the x/y position offset relative to the top-left of the camera view.
* If the parent of this Group is also `fixedToCamera` then the offset here is in addition to that and should typically be disabled.
* @property {Phaser.Point} cameraOffset
*/
this.cameraOffset = new Phaser.Point();
/**
* The hash array is an array belonging to this Group into which you can add any of its children via Group.addToHash and Group.removeFromHash.
*
* Only children of this Group can be added to and removed from the hash.
*
* This hash is used automatically by Phaser Arcade Physics in order to perform non z-index based destructive sorting.
* However if you don't use Arcade Physics, or this isn't a physics enabled Group, then you can use the hash to perform your own
* sorting and filtering of Group children without touching their z-index (and therefore display draw order)
*
* @property {array} hash
*/
this.hash = [];
/**
* The property on which children are sorted.
* @property {string} _sortProperty
* @private
*/
this._sortProperty = 'z';
};
Phaser.Group.prototype = Object.create(PIXI.DisplayObjectContainer.prototype);
Phaser.Group.prototype.constructor = Phaser.Group;
/**
* A returnType value, as specified in {@link #iterate} eg.
* @constant
* @type {integer}
*/
Phaser.Group.RETURN_NONE = 0;
/**
* A returnType value, as specified in {@link #iterate} eg.
* @constant
* @type {integer}
*/
Phaser.Group.RETURN_TOTAL = 1;
/**
* A returnType value, as specified in {@link #iterate} eg.
* @constant
* @type {integer}
*/
Phaser.Group.RETURN_CHILD = 2;
/**
* A returnType value, as specified in {@link #iterate} eg.
* @constant
* @type {integer}
*/
Phaser.Group.RETURN_ALL = 3;
/**
* A sort ordering value, as specified in {@link #sort} eg.
* @constant
* @type {integer}
*/
Phaser.Group.SORT_ASCENDING = -1;
/**
* A sort ordering value, as specified in {@link #sort} eg.
* @constant
* @type {integer}
*/
Phaser.Group.SORT_DESCENDING = 1;
/**
* Adds an existing object as the top child in this group.
*
* The child is automatically added to the top of the group, and is displayed above every previous child.
*
* Or if the _optional_ index is specified, the child is added at the location specified by the index value,
* this allows you to control child ordering.
*
* If the child was already in this Group, it is simply returned, and nothing else happens to it.
*
* If `Group.enableBody` is set, then a physics body will be created on the object, so long as one does not already exist.
*
* If `Group.inputEnableChildren` is set, then an Input Handler will be created on the object, so long as one does not already exist.
*
* Use {@link #create} to create and add a new child.
*
* @method Phaser.Group#add
* @param {DisplayObject} child - The display object to add as a child.
* @param {boolean} [silent=false] - If true the child will not dispatch the `onAddedToGroup` event.
* @param {integer} [index] - The index within the group to insert the child to. Where 0 is the bottom of the Group.
* @return {DisplayObject} The child that was added to the group.
*/
Phaser.Group.prototype.add = function (child, silent, index)
{
if (silent === undefined) { silent = false; }
if (child.parent === this)
{
return child;
}
if (child.body && child.parent && child.parent.hash)
{
child.parent.removeFromHash(child);
}
if (index === undefined)
{
child.z = this.children.length;
this.addChild(child);
}
else
{
this.addChildAt(child, index);
this.updateZ();
}
if (this.enableBody && child.hasOwnProperty('body') && child.body === null)
{
this.game.physics.enable(child, this.physicsBodyType);
}
else if (child.body)
{
this.addToHash(child);
}
if (this.inputEnableChildren && (!child.input || child.inputEnabled))
{
child.inputEnabled = true;
}
if (!silent && child.events)
{
child.events.onAddedToGroup$dispatch(child, this);
}
if (this.cursor === null)
{
this.cursor = child;
}
return child;
};
/**
* Adds an existing object to this group.
*
* The child is added to the group at the location specified by the index value, this allows you to control child ordering.
*
* If `Group.enableBody` is set, then a physics body will be created on the object, so long as one does not already exist.
*
* If `Group.inputEnableChildren` is set, then an Input Handler will be created on the object, so long as one does not already exist.
*
* @method Phaser.Group#addAt
* @param {DisplayObject} child - The display object to add as a child.
* @param {integer} [index=0] - The index within the group to insert the child to.
* @param {boolean} [silent=false] - If true the child will not dispatch the `onAddedToGroup` event.
* @return {DisplayObject} The child that was added to the group.
*/
Phaser.Group.prototype.addAt = function (child, index, silent)
{
return this.add(child, silent, index);
};
/**
* Adds a child of this Group into the hash array.
* This call will return false if the child is not a child of this Group, or is already in the hash.
*
* @method Phaser.Group#addToHash
* @param {DisplayObject} child - The display object to add to this Groups hash. Must be a member of this Group already and not present in the hash.
* @return {boolean} True if the child was successfully added to the hash, otherwise false.
*/
Phaser.Group.prototype.addToHash = function (child)
{
if (child.parent === this)
{
var index = this.hash.indexOf(child);
if (index === -1)
{
this.hash.push(child);
return true;
}
}
return false;
};
/**
* Removes a child of this Group from the hash array.
* This call will return false if the child is not in the hash.
*
* @method Phaser.Group#removeFromHash
* @param {DisplayObject} child - The display object to remove from this Groups hash. Must be a member of this Group and in the hash.
* @return {boolean} True if the child was successfully removed from the hash, otherwise false.
*/
Phaser.Group.prototype.removeFromHash = function (child)
{
if (child)
{
var index = this.hash.indexOf(child);
if (index !== -1)
{
this.hash.splice(index, 1);
return true;
}
}
return false;
};
/**
* Adds an array of existing Display Objects to this Group.
*
* The Display Objects are automatically added to the top of this Group, and will render on-top of everything already in this Group.
*
* As well as an array you can also pass another Group as the first argument. In this case all of the children from that
* Group will be removed from it and added into this Group.
*
* If `Group.enableBody` is set, then a physics body will be created on the objects, so long as one does not already exist.
*
* If `Group.inputEnableChildren` is set, then an Input Handler will be created on the objects, so long as one does not already exist.
*
* @method Phaser.Group#addMultiple
* @param {DisplayObject[]|Phaser.Group} children - An array of display objects or a Phaser.Group. If a Group is given then *all* children will be moved from it.
* @param {boolean} [silent=false] - If true the children will not dispatch the `onAddedToGroup` event.
* @return {DisplayObject[]|Phaser.Group} The array of children or Group of children that were added to this Group.
*/
Phaser.Group.prototype.addMultiple = function (children, silent)
{
if (children instanceof Phaser.Group)
{
children.moveAll(this, silent);
}
else if (Array.isArray(children))
{
for (var i = 0; i < children.length; i++)
{
this.add(children[i], silent);
}
}
return children;
};
/**
* Returns the child found at the given index within this group.
*
* @method Phaser.Group#getAt
* @param {integer} index - The index to return the child from.
* @return {DisplayObject|integer} The child that was found at the given index, or -1 for an invalid index.
*/
Phaser.Group.prototype.getAt = function (index)
{
if (index < 0 || index >= this.children.length)
{
return -1;
}
else
{
return this.getChildAt(index);
}
};
/**
* Creates a new Phaser.Sprite object and adds it to the top of this group.
*
* Use {@link #classType} to change the type of object created.
*
* The child is automatically added to the top of the group, and is displayed above every previous child.
*
* Or if the _optional_ index is specified, the child is added at the location specified by the index value,
* this allows you to control child ordering.
*
* If `Group.enableBody` is set, then a physics body will be created on the object, so long as one does not already exist.
*
* If `Group.inputEnableChildren` is set, then an Input Handler will be created on the object, so long as one does not already exist.
*
* @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|Phaser.RenderTexture|Phaser.BitmapData|Phaser.Video|PIXI.Texture} [key] - This is the image or texture used by the Sprite during rendering. It can be a string which is a reference to the Cache Image entry, or an instance of a RenderTexture, BitmapData, Video or PIXI.Texture.
* @param {string|number} [frame] - If this Sprite is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index.
* @param {boolean} [exists=true] - The default exists state of the Sprite.
* @param {integer} [index] - The index within the group to insert the child to. Where 0 is the bottom of the Group.
* @return {DisplayObject} The child that was created: will be a {@link Phaser.Sprite} unless {@link #classType} has been changed.
*/
Phaser.Group.prototype.create = function (x, y, key, frame, exists, index)
{
if (exists === undefined) { exists = true; }
var child = new this.classType(this.game, x, y, key, frame);
child.exists = exists;
child.visible = exists;
child.alive = exists;
return this.add(child, false, index);
};
/**
* Creates multiple Phaser.Sprite objects and adds them to the top of this Group.
*
* This method is useful if you need to quickly generate a pool of sprites, such as bullets.
*
* Use {@link #classType} to change the type of object created.
*
* You can provide an array as the `key` and / or `frame` arguments. When you do this
* it will create `quantity` Sprites for every key (and frame) in the arrays.
*
* For example:
*
* `createMultiple(25, ['ball', 'carrot'])`
*
* In the above code there are 2 keys (ball and carrot) which means that 50 sprites will be
* created in total, 25 of each. You can also have the `frame` as an array:
*
* `createMultiple(5, 'bricks', [0, 1, 2, 3])`
*
* In the above there is one key (bricks), which is a sprite sheet. The frames array tells
* this method to use frames 0, 1, 2 and 3. So in total it will create 20 sprites, because
* the quantity was set to 5, so that is 5 brick sprites of frame 0, 5 brick sprites with
* frame 1, and so on.
*
* If you set both the key and frame arguments to be arrays then understand it will create
* a total quantity of sprites equal to the size of both arrays times each other. I.e.:
*
* `createMultiple(20, ['diamonds', 'balls'], [0, 1, 2])`
*
* The above will create 20 'diamonds' of frame 0, 20 with frame 1 and 20 with frame 2.
* It will then create 20 'balls' of frame 0, 20 with frame 1 and 20 with frame 2.
* In total it will have created 120 sprites.
*
* By default the Sprites will have their `exists` property set to `false`, and they will be
* positioned at 0x0, relative to the `Group.x / y` values.
*
* If `Group.enableBody` is set, then a physics body will be created on the objects, so long as one does not already exist.
*
* If `Group.inputEnableChildren` is set, then an Input Handler will be created on the objects, so long as one does not already exist.
*
* @method Phaser.Group#createMultiple
* @param {integer} quantity - The number of Sprites to create.
* @param {string|array} key - The Cache key of the image that the Sprites will use. Or an Array of keys. See the description for details on how the quantity applies when arrays are used.
* @param {integer|string|array} [frame=0] - If the Sprite image contains multiple frames you can specify which one to use here. Or an Array of frames. See the description for details on how the quantity applies when arrays are used.
* @param {boolean} [exists=false] - The default exists state of the Sprite.
* @param {function} [callback] - The function that will be called for each applicable child. It will be passed the new child and the loop index (0 through quantity - 1).
* @param {object} [callbackContext] - The context in which the function should be called (usually 'this'). The default context is the new child.
* @return {array} An array containing all of the Sprites that were created.
*/
Phaser.Group.prototype.createMultiple = function (quantity, key, frame, exists, callback, callbackContext)
{
if (frame === undefined) { frame = 0; }
if (exists === undefined) { exists = false; }
if (!Array.isArray(key))
{
key = [ key ];
}
if (!Array.isArray(frame))
{
frame = [ frame ];
}
var _this = this;
var children = [];
key.forEach(function (singleKey)
{
frame.forEach(function (singleFrame)
{
for (var i = 0; i < quantity; i++)
{
var child = _this.create(0, 0, singleKey, singleFrame, exists);
if (callback) { callback.call(callbackContext || child, child, i); }
children.push(child);
}
});
});
return children;
};
/**
* Internal method that re-applies all of the children's Z values.
*
* This must be called whenever children ordering is altered so that their `z` indices are correctly updated.
*
* @method Phaser.Group#updateZ
* @protected
*/
Phaser.Group.prototype.updateZ = function ()
{
var i = this.children.length;
while (i--)
{
this.children[i].z = i;
}
};
/**
* This method iterates through all children in the Group (regardless if they are visible or exist)
* and then changes their position so they are arranged in a Grid formation. Children must have
* the `alignTo` method in order to be positioned by this call. All default Phaser Game Objects have
* this.
*
* The grid dimensions are determined by the first four arguments. The `width` and `height` arguments
* relate to the width and height of the grid respectively.
*
* For example if the Group had 100 children in it:
*
* `Group.align(10, 10, 32, 32)`
*
* This will align all of the children into a grid formation of 10x10, using 32 pixels per
* grid cell. If you want a wider grid, you could do:
*
* `Group.align(25, 4, 32, 32)`
*
* This will align the children into a grid of 25x4, again using 32 pixels per grid cell.
*
* You can choose to set _either_ the `width` or `height` value to -1. Doing so tells the method
* to keep on aligning children until there are no children left. For example if this Group had
* 48 children in it, the following:
*
* `Group.align(-1, 8, 32, 32)`
*
* ... will align the children so that there are 8 children vertically (the second argument),
* and each row will contain 6 sprites, except the last one, which will contain 5 (totaling 48)
*
* You can also do:
*
* `Group.align(10, -1, 32, 32)`
*
* In this case it will create a grid 10 wide, and as tall as it needs to be in order to fit
* all of the children in.
*
* The `position` property allows you to control where in each grid cell the child is positioned.
* This is a constant and can be one of `Phaser.TOP_LEFT` (default), `Phaser.TOP_CENTER`,
* `Phaser.TOP_RIGHT`, `Phaser.LEFT_CENTER`, `Phaser.CENTER`, `Phaser.RIGHT_CENTER`,
* `Phaser.BOTTOM_LEFT`, `Phaser.BOTTOM_CENTER` or `Phaser.BOTTOM_RIGHT`.
*
* The final argument; `offset` lets you start the alignment from a specific child index.
*
* @method Phaser.Group#align
* @param {integer} width - The width of the grid in items (not pixels). Set to -1 for a dynamic width. If -1 then you must set an explicit height value.
* @param {integer} height - The height of the grid in items (not pixels). Set to -1 for a dynamic height. If -1 then you must set an explicit width value.
* @param {integer} cellWidth - The width of each grid cell, in pixels.
* @param {integer} cellHeight - The height of each grid cell, in pixels.
* @param {integer} [position] - The position constant. One of `Phaser.TOP_LEFT` (default), `Phaser.TOP_CENTER`, `Phaser.TOP_RIGHT`, `Phaser.LEFT_CENTER`, `Phaser.CENTER`, `Phaser.RIGHT_CENTER`, `Phaser.BOTTOM_LEFT`, `Phaser.BOTTOM_CENTER` or `Phaser.BOTTOM_RIGHT`.
* @param {integer} [offset=0] - Optional index to start the alignment from. Defaults to zero, the first child in the Group, but can be set to any valid child index value.
* @return {boolean} True if the Group children were aligned, otherwise false.
*/
Phaser.Group.prototype.align = function (width, height, cellWidth, cellHeight, position, offset)
{
if (position === undefined) { position = Phaser.TOP_LEFT; }
if (offset === undefined) { offset = 0; }
if (this.children.length === 0 || offset > this.children.length || (width === -1 && height === -1))
{
return false;
}
var r = new Phaser.Rectangle(0, 0, cellWidth, cellHeight);
var w = (width * cellWidth);
var h = (height * cellHeight);
for (var i = offset; i < this.children.length; i++)
{
var child = this.children[i];
if (child.alignIn)
{
child.alignIn(r, position);
}
else
{
continue;
}
if (width === -1)
{
// We keep laying them out horizontally until we've done them all
r.y += cellHeight;
if (r.y === h)
{
r.x += cellWidth;
r.y = 0;
}
}
else if (height === -1)
{
// We keep laying them out vertically until we've done them all
r.x += cellWidth;
if (r.x === w)
{
r.x = 0;
r.y += cellHeight;
}
}
else
{
// We keep laying them out until we hit the column limit
r.x += cellWidth;
if (r.x === w)
{
r.x = 0;
r.y += cellHeight;
if (r.y === h)
{
// We've hit the column limit, so return, even if there are children left
return true;
}
}
}
}
return true;
};
/**
* Sets the group cursor to the first child 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 {integer} [index=0] - Set the cursor to point to a specific index.
* @return {any} The child the cursor now points to.
*/
Phaser.Group.prototype.resetCursor = function (index)
{
if (index === undefined) { index = 0; }
if (index > this.children.length - 1)
{
index = 0;
}
if (this.cursor)
{
this.cursorIndex = index;
this.cursor = this.children[this.cursorIndex];
return this.cursor;
}
};
/**
* Advances the group cursor to the next (higher) object in the group.
*
* If the cursor is at the end of the group (top child) it is moved the start of the group (bottom child).
*
* @method Phaser.Group#next
* @return {any} The child the cursor now points to.
*/
Phaser.Group.prototype.next = function ()
{
if (this.cursor)
{
// Wrap the cursor?
if (this.cursorIndex >= this.children.length - 1)
{
this.cursorIndex = 0;
}
else
{
this.cursorIndex++;
}
this.cursor = this.children[this.cursorIndex];
return this.cursor;
}
};
/**
* Moves the group cursor to the previous (lower) child in the group.
*
* If the cursor is at the start of the group (bottom child) it is moved to the end (top child).
*
* @method Phaser.Group#previous
* @return {any} The child the cursor now points to.
*/
Phaser.Group.prototype.previous = function ()
{
if (this.cursor)
{
// Wrap the cursor?
if (this.cursorIndex === 0)
{
this.cursorIndex = this.children.length - 1;
}
else
{
this.cursorIndex--;
}
this.cursor = this.children[this.cursorIndex];
return this.cursor;
}
};
/**
* Swaps the position of two children in this group.
*
* Both children must be in this group, a child cannot be swapped with itself, and unparented children cannot be swapped.
*
* @method Phaser.Group#swap
* @param {any} child1 - The first child to swap.
* @param {any} child2 - The second child to swap.
*/
Phaser.Group.prototype.swap = function (child1, child2)
{
this.swapChildren(child1, child2);
this.updateZ();
};
/**
* Brings the given child to the top of this group so it renders above all other children.
*
* @method Phaser.Group#bringToTop
* @param {any} child - The child to bring to the top of this group.
* @return {any} The child that was moved.
*/
Phaser.Group.prototype.bringToTop = function (child)
{
if (child.parent === this && this.getIndex(child) < this.children.length)
{
this.remove(child, false, true);
this.add(child, true);
}
return child;
};
/**
* Alias for {@link Phaser.Group#bringToTop}.
* @private
*/
Phaser.Group.prototype.bringChildToTop = Phaser.Group.prototype.bringToTop;
/**
* Sends the given child to the bottom of this group so it renders below all other children.
*
* @method Phaser.Group#sendToBack
* @param {any} child - The child to send to the bottom of this group.
* @return {any} The child that was moved.
*/
Phaser.Group.prototype.sendToBack = function (child)
{
if (child.parent === this && this.getIndex(child) > 0)
{
this.remove(child, false, true);
this.addAt(child, 0, true);
}
return child;
};
/**
* Alias for {@link Phaser.Group#sendToBack}.
* @private
*/
Phaser.Group.prototype.sendChildToBack = Phaser.Group.prototype.sendToBack;
/**
* Moves the given child up one place in this group unless it's already at the top.
*
* @method Phaser.Group#moveUp
* @param {any} child - The child to move up in the group.
* @return {any} 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)
{
this.swap(child, b);
}
}
return child;
};
/**
* Moves the given child down one place in this group unless it's already at the bottom.
*
* @method Phaser.Group#moveDown
* @param {any} child - The child to move down in the group.
* @return {any} 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)
{
this.swap(child, b);
}
}
return child;
};
/**
* Positions the child found at the given index within this group to the given x and y coordinates.
*
* @method Phaser.Group#xy
* @param {integer} 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;
}
};
/**
* Reverses all children in this group.
*
* This operation applies only to immediate children and does not propagate to subgroups.
*
* @method Phaser.Group#reverse
*/
Phaser.Group.prototype.reverse = function ()
{
this.children.reverse();
this.updateZ();
};
/**
* Get the index position of the given child in this group, which should match the child's `z` property.
*
* @method Phaser.Group#getIndex
* @param {any} child - The child to get the index for.
* @return {integer} The index of the child or -1 if it's not a member of this group.
*/
Phaser.Group.prototype.getIndex = function (child)
{
return this.children.indexOf(child);
};
/**
* Searches the Group for the first instance of a child with the `name`
* property matching the given argument. Should more than one child have
* the same name only the first instance is returned.
*
* @method Phaser.Group#getByName
* @param {string} name - The name to search for.
* @return {any} The first child with a matching name, or null if none were found.
*/
Phaser.Group.prototype.getByName = function (name)
{
return this.getFirst('name', name);
};
/**
* Replaces a child of this Group with the given newChild. The newChild cannot be a member of this Group.
*
* If `Group.enableBody` is set, then a physics body will be created on the object, so long as one does not already exist.
*
* If `Group.inputEnableChildren` is set, then an Input Handler will be created on the object, so long as one does not already exist.
*
* @method Phaser.Group#replace
* @param {any} oldChild - The child in this group that will be replaced.
* @param {any} newChild - The child to be inserted into this group.
* @return {any} Returns the oldChild that was replaced within this group.
*/
Phaser.Group.prototype.replace = function (oldChild, newChild)
{
var index = this.getIndex(oldChild);
if (index !== -1)
{
if (newChild.parent)
{
if (newChild.parent instanceof Phaser.Group)
{
newChild.parent.remove(newChild);
}
else
{
newChild.parent.removeChild(newChild);
}
}
this.remove(oldChild);
this.addAt(newChild, index);
return oldChild;
}
};
/**
* Checks if the child has the given property.
*
* Will scan up to 4 levels deep only.
*
* @method Phaser.Group#hasProperty
* @param {any} child - The child to check for the existence of the property on.
* @param {string[]} 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;
if (len === 1 && key[0] in child)
{
return true;
}
else if (len === 2 && key[0] in child && key[1] in child[key[0]])
{
return true;
}
else if (len === 3 && key[0] in child && key[1] in child[key[0]] && key[2] in child[key[0]][key[1]])
{
return true;
}
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]])
{
return true;
}
return false;
};
/**
* Sets a property to the given value on the child. The operation parameter controls how the value is set.
*
* The operations are:
* - 0: set the existing value to the given value; if force is `true` a new property will be created if needed
* - 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.
*
* @method Phaser.Group#setProperty
* @param {any} 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 {any} value - The value that will be set.
* @param {integer} [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.
* @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.
*/
Phaser.Group.prototype.setProperty = function (child, key, value, operation, force)
{
if (force === undefined) { force = false; }
operation = operation || 0;
// 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.
/*
* 0 = Equals
* 1 = Add
* 2 = Subtract
* 3 = Multiply
* 4 = Divide
*/
/*
* 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.
*/
if (!this.hasProperty(child, key) && (!force || operation > 0))
{
return false;
}
var len = key.length;
if (len === 1)
{
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; }
}
else if (len === 2)
{
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; }
}
else if (len === 3)
{
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; }
}
else if (len === 4)
{
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; }
}
return true;
};
/**
* Checks a property for the given value on the child.
*
* @method Phaser.Group#checkProperty
* @param {any} child - The child to check the property value on.
* @param {string} key - The property, as a string, to be checked. For example: 'body.velocity.x'
* @param {any} value - The value that will be checked.
* @param {boolean} [force=false] - Also return false if the property is missing or undefined (regardless of the `value` argument).
* @return {boolean} True if `child` is a child of this Group and the property was equal to value, false if not.
*/
Phaser.Group.prototype.checkProperty = function (child, key, value, force)
{
if (force === undefined) { force = false; }
if (this !== child.parent)
{
return false;
}
var result = Phaser.Utils.getProperty(child, key);
if (((result === undefined) && force) || (result !== value))
{
return false;
}
return true;
};
/**
* 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 {any} 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 {integer} [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.
* @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.
*/
Phaser.Group.prototype.set = function (child, key, value, checkAlive, checkVisible, operation, force)
{
if (force === undefined) { force = false; }
key = key.split('.');
if (checkAlive === undefined) { checkAlive = false; }
if (checkVisible === undefined) { checkVisible = false; }
if ((checkAlive === false || (checkAlive && child.alive)) && (checkVisible === false || (checkVisible && child.visible)))
{
return this.setProperty(child, key, value, operation, force);
}
};
/**
* Quickly set the same property across all children of this group to a new value.
*
* 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`.
*
* 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 {any} 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 {integer} [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.
* @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.
*/
Phaser.Group.prototype.setAll = function (key, value, checkAlive, checkVisible, operation, force)
{
if (checkAlive === undefined) { checkAlive = false; }
if (checkVisible === undefined) { checkVisible = false; }
if (force === undefined) { force = false; }
key = key.split('.');
operation = operation || 0;
var len = this.children.length;
for (var i = 0; i < len; i++)
{
var child = this.children[i];
if ((!checkAlive || child.alive) && (!checkVisible || child.visible))
{
this.setProperty(child, key, value, operation, force);
}
}
};
/**
* 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 `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 {any} 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 {integer} [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.
* @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.
*/
Phaser.Group.prototype.setAllChildren = function (key, value, checkAlive, checkVisible, operation, force)
{
if (checkAlive === undefined) { checkAlive = false; }
if (checkVisible === undefined) { checkVisible = false; }
if (force === undefined) { force = false; }
operation = operation || 0;
var len = this.children.length;
for (var i = 0; i < len; i++)
{
var child = this.children[i];
if ((!checkAlive || child.alive) && (!checkVisible || child.visible))
{
if (child instanceof Phaser.Group)
{
child.setAllChildren(key, value, checkAlive, checkVisible, operation, force);
}
else
{
this.setProperty(child, key.split('.'), value, operation, force);
}
}
}
};
/**
* Test that the same property across all children of this group is equal to the given value.
*
* This call doesn't descend down children, so if you have a Group inside of this group, the property will be checked on the group but not its children.
*
* @method Phaser.Group#checkAll
* @param {string} key - The property, as a string, to be checked. For example: 'body.velocity.x'
* @param {any} value - The value that will be checked.
* @param {boolean} [checkAlive=false] - If set then only children with alive=true will be checked. This includes any Groups that are children.
* @param {boolean} [checkVisible=false] - If set then only children with visible=true will be checked. This includes any Groups that are children.
* @param {boolean} [force=false] - Also return false if the property is missing or undefined (regardless of the `value` argument).
* @return {boolean} - True if all eligible children have the given property value (but see `force`); otherwise false.
*/
Phaser.Group.prototype.checkAll = function (key, value, checkAlive, checkVisible, force)
{
if (checkAlive === undefined) { checkAlive = false; }
if (checkVisible === undefined) { checkVisible = false; }
if (force === undefined) { force = false; }
for (var i = 0; i < this.child