Skip to content

Commit e691556

Browse files
committed
Layer.createMultiple can now take an array of config objects.
1 parent 5c0f550 commit e691556

2 files changed

Lines changed: 41 additions & 56 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '262e7d70-1411-11e7-8d7e-9ffab0018bd6'
2+
build: '448d6cb0-1414-11e7-9400-83d0899b34cd'
33
};
44
module.exports = CHECKSUM;

v3/src/gameobjects/layer/Layer.js

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -54,62 +54,30 @@ var Layer = new Class({
5454
return child;
5555
},
5656

57-
/**
58-
* Creates multiple Phaser.Sprite objects and adds them to the top of this Group.
59-
*
60-
* This method is useful if you need to quickly generate a pool of sprites, such as bullets.
61-
*
62-
* Use {@link #classType} to change the type of object created.
63-
*
64-
* You can provide an array as the `key` and / or `frame` arguments. When you do this
65-
* it will create `quantity` Sprites for every key (and frame) in the arrays.
66-
*
67-
* For example:
68-
*
69-
* `createMultiple(25, ['ball', 'carrot'])`
70-
*
71-
* In the above code there are 2 keys (ball and carrot) which means that 50 sprites will be
72-
* created in total, 25 of each. You can also have the `frame` as an array:
73-
*
74-
* `createMultiple(5, 'bricks', [0, 1, 2, 3])`
75-
*
76-
* In the above there is one key (bricks), which is a sprite sheet. The frames array tells
77-
* this method to use frames 0, 1, 2 and 3. So in total it will create 20 sprites, because
78-
* the quantity was set to 5, so that is 5 brick sprites of frame 0, 5 brick sprites with
79-
* frame 1, and so on.
80-
*
81-
* If you set both the key and frame arguments to be arrays then understand it will create
82-
* a total quantity of sprites equal to the size of both arrays times each other. I.e.:
83-
*
84-
* `createMultiple(20, ['diamonds', 'balls'], [0, 1, 2])`
85-
*
86-
* The above will create 20 'diamonds' of frame 0, 20 with frame 1 and 20 with frame 2.
87-
* It will then create 20 'balls' of frame 0, 20 with frame 1 and 20 with frame 2.
88-
* In total it will have created 120 sprites.
89-
*
90-
* By default the Sprites will be positioned at 0x0.
91-
*
92-
* @method Phaser.Group#createMultiple
93-
* @param {integer} quantity - The number of Sprites to create.
94-
* @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.
95-
* @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.
96-
* @param {boolean} [exists=false] - The default exists state of the Sprite.
97-
* @return {array} An array containing all of the Sprites that were created.
98-
*/
99-
createMultiple: function (key, frame, options)
100-
{
101-
if (frame === undefined) { frame = null; }
102-
57+
createFromConfig: function (options)
58+
{
59+
var key = GetObjectValue(options, 'key', undefined);
60+
var frame = GetObjectValue(options, 'frame', null);
10361
var visible = GetObjectValue(options, 'visible', true);
10462

105-
if (!Array.isArray(key))
63+
var entries = [];
64+
65+
// Can't do anything without at least a key
66+
if (key === undefined)
10667
{
107-
key = [ key ];
68+
return entries;
10869
}
109-
110-
if (!Array.isArray(frame))
70+
else
11171
{
112-
frame = [ frame ];
72+
if (!Array.isArray(key))
73+
{
74+
key = [ key ];
75+
}
76+
77+
if (!Array.isArray(frame))
78+
{
79+
frame = [ frame ];
80+
}
11381
}
11482

11583
// Build an array of key frame pairs to loop through
@@ -121,8 +89,6 @@ var Layer = new Class({
12189
var quantity = GetObjectValue(options, 'quantity', 1);
12290
var max = GetObjectValue(options, 'max', 0);
12391

124-
var entries = [];
125-
12692
var range = Range(key, frame, {
12793
max: max,
12894
qty: quantity,
@@ -132,9 +98,9 @@ var Layer = new Class({
13298
yoyo: yoyo
13399
});
134100

135-
for (var i = 0; i < range.length; i++)
101+
for (var c = 0; c < range.length; c++)
136102
{
137-
entries.push(this.create(0, 0, range[i].a, range[i].b, visible));
103+
entries.push(this.create(0, 0, range[c].a, range[c].b, visible));
138104
}
139105

140106
// Post-creation options (applied only to those items created in this call):
@@ -166,6 +132,25 @@ var Layer = new Class({
166132
return entries;
167133
},
168134

135+
createMultiple: function (config)
136+
{
137+
if (!Array.isArray(config))
138+
{
139+
config = [ config ];
140+
}
141+
142+
var output = [];
143+
144+
for (var i = 0; i < config.length; i++)
145+
{
146+
var entries = this.createFromConfig(config[i]);
147+
148+
output = output.concat(entries);
149+
}
150+
151+
return output;
152+
},
153+
169154
remove: function (child)
170155
{
171156
this.children.delete(child);

0 commit comments

Comments
 (0)