Skip to content

Commit 32cf1f1

Browse files
committed
Group.createMultiple can now accept Arrays for both the key and frame arguments. This allows you to create multiple sprites using each key and/or frame in the arrays, which is a great and quick way to build diverse Groups. See the JSDocs for complete details and code examples.
1 parent 2aa2c67 commit 32cf1f1

3 files changed

Lines changed: 86 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
391391
* Group.createMultiple now returns an Array containing references to all of the children that the method created.
392392
* Cache.getJSON will now return an Array if the `key` you provided points to an array instead of an Object (thanks @drhayes #2552 #2551)
393393
* Phaser.Matrix if passed a 0 value would consider it falsy, and replace it with the default by mistake. It now checks if the arguments are `undefined` or `null` and only then sets the defaults (thanks mmcs)
394+
* Group.createMultiple can now accept Arrays for both the `key` and `frame` arguments. This allows you to create multiple sprites using each key and/or frame in the arrays, which is a great and quick way to build diverse Groups. See the JSDocs for complete details and code examples.
394395

395396
### Bug Fixes
396397

src/core/Group.js

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,36 @@ Phaser.Group.prototype.create = function (x, y, key, frame, exists, index) {
573573

574574
/**
575575
* Creates multiple Phaser.Sprite objects and adds them to the top of this Group.
576+
*
577+
* This method is useful if you need to quickly generate a pool of sprites, such as bullets.
576578
*
577579
* Use {@link #classType} to change the type of object created.
578580
*
579-
* This method is useful if you need to quickly generate a pool of identical sprites, such as bullets.
581+
* You can provide an array as the `key` and / or `frame` arguments. When you do this
582+
* it will create `quantity` Sprites for every key (and frame) in the arrays.
583+
*
584+
* For example:
585+
*
586+
* `createMultiple(25, ['ball', 'carrot'])`
587+
*
588+
* In the above code there are 2 keys (ball and carrot) which means that 50 sprites will be
589+
* created in total, 25 of each. You can also have the `frame` as an array:
590+
*
591+
* `createMultiple(5, 'bricks', [0, 1, 2, 3])`
592+
*
593+
* In the above there is one key (bricks), which is a sprite sheet. The frames array tells
594+
* this method to use frames 0, 1, 2 and 3. So in total it will create 20 sprites, because
595+
* the quantity was set to 5, so that is 5 brick sprites of frame 0, 5 brick sprites with
596+
* frame 1, and so on.
597+
*
598+
* If you set both the key and frame arguments to be arrays then understand it will create
599+
* a total quantity of sprites equal to the size of both arrays times each other. I.e.:
600+
*
601+
* `createMultiple(20, ['diamonds', 'balls'], [0, 1, 2])`
602+
*
603+
* The above will create 20 'diamonds' of frame 0, 20 with frame 1 and 20 with frame 2.
604+
* It will then create 20 'balls' of frame 0, 20 with frame 1 and 20 with frame 2.
605+
* In total it will have created 120 sprites.
580606
*
581607
* By default the Sprites will have their `exists` property set to `false`, and they will be
582608
* positioned at 0x0, relative to the `Group.x / y` values.
@@ -587,22 +613,42 @@ Phaser.Group.prototype.create = function (x, y, key, frame, exists, index) {
587613
*
588614
* @method Phaser.Group#createMultiple
589615
* @param {integer} quantity - The number of Sprites to create.
590-
* @param {string} key - The Game.cache key of the image that this Sprite will use.
591-
* @param {integer|string} [frame] - If the Sprite image contains multiple frames you can specify which one to use here.
616+
* @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.
617+
* @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.
592618
* @param {boolean} [exists=false] - The default exists state of the Sprite.
593619
* @return {array} An array containing all of the Sprites that were created.
594620
*/
595621
Phaser.Group.prototype.createMultiple = function (quantity, key, frame, exists) {
596622

623+
if (frame === undefined) { frame = 0; }
597624
if (exists === undefined) { exists = false; }
598625

599-
var children = [];
626+
if (!Array.isArray(key))
627+
{
628+
key = [ key ];
629+
}
600630

601-
for (var i = 0; i < quantity; i++)
631+
if (!Array.isArray(frame))
602632
{
603-
children.push(this.create(0, 0, key, frame, exists));
633+
frame = [ frame ];
604634
}
605635

636+
var _this = this;
637+
var children = [];
638+
639+
key.forEach(function(singleKey) {
640+
641+
frame.forEach(function(singleFrame) {
642+
643+
for (var i = 0; i < quantity; i++)
644+
{
645+
children.push(_this.create(0, 0, singleKey, singleFrame, exists));
646+
}
647+
648+
});
649+
650+
});
651+
606652
return children;
607653

608654
};
@@ -626,6 +672,37 @@ Phaser.Group.prototype.updateZ = function () {
626672

627673
};
628674

675+
Phaser.Group.prototype.align = function (rows, columns, cellWidth, cellHeight, alignType) {
676+
677+
if (this.children.length === 0) { return; }
678+
679+
var x = 0;
680+
var y = 0;
681+
var c = 0;
682+
683+
for (var i = 0; i < this.children.length; i++)
684+
{
685+
var child = this.children[i];
686+
687+
var b = child.getBounds();
688+
689+
// First let's just align based on the top-left of the child
690+
x = c * cellWidth;
691+
692+
child.x = x;
693+
child.y = y;
694+
695+
c++;
696+
697+
if (c === rows)
698+
{
699+
c = 0;
700+
y += cellHeight;
701+
}
702+
}
703+
704+
};
705+
629706
/**
630707
* Sets the group cursor to the first child in the group.
631708
*

typescript/phaser.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path="pixi.d.ts" />
22
/// <reference path="p2.d.ts" />
33

4-
// Type definitions for Phaser 2.4.9 - 14th June 2016
4+
// Type definitions for Phaser 2.4.9 - 15th June 2016
55
// Project: https://github.com/photonstorm/phaser
66

77
declare module "phaser" {
@@ -1727,7 +1727,7 @@ declare module "phaser" {
17271727
countDead(): number;
17281728
countLiving(): number;
17291729
create(x: number, y: number, key?: string | Phaser.RenderTexture | Phaser.BitmapData | Phaser.Video | PIXI.Texture, frame?: string | number, exists?: boolean, index?: number): any;
1730-
createMultiple(quantity: number, key: string, frame?: any, exists?: boolean): any[];
1730+
createMultiple(quantity: number, key: string | string[], frame?: any | any[], exists?: boolean): any[];
17311731
customSort(sortHandler: Function, context?: any): void;
17321732
destroy(destroyChildren?: boolean, soft?: boolean): void;
17331733
divideAll(property: string, amount: number, checkAlive?: boolean, checkVisible?: boolean): void;

0 commit comments

Comments
 (0)