Skip to content

Commit ba72755

Browse files
committed
Emitter.flow now works in a slightly different (and more useful!) way. You can now specify a quantity and a total. The quantity controls how many particles are emitted every time the flow frequency is met. The total controls how many particles will be emitted in total. You can set total to be -1 and it will carry on emitting at the given frequency forever (also fixes phaserjs#1598 thanks @brianbunch)
1 parent 9879fc6 commit ba72755

2 files changed

Lines changed: 89 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ We've also removed functions and properties from Pixi classes that Phaser doesn'
116116
* Sound.loopFull is a new method that will start playback of the Sound and set it to loop in its entirety.
117117
* Sprite.left, Sprite.right, Sprite.top, Sprite.bottom are new properties that contain the totals of the Sprite position and dimensions, adjusted for the anchor.
118118
* Sprite.offsetX and Sprite.offsetY contain the offsets from the Sprite.x/y coordinates to the top-left of the Sprite, taking anchor into consideration.
119+
* Emitter.flow now works in a slightly different (and more useful!) way. You can now specify a `quantity` and a `total`. The `quantity` controls how many particles are emitted every time the flow frequency is met. The `total` controls how many particles will be emitted in total. You can set `total` to be -1 and it will carry on emitting at the given frequency forever (also fixes #1598 thanks @brianbunch)
119120

120121
### Updates
121122

src/particles/arcade/Emitter.js

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,18 @@ Phaser.Particles.Arcade.Emitter = function (game, x, y, maxParticles) {
228228
*/
229229
this._counter = 0;
230230

231+
/**
232+
* @property {number} _flowQuantity - Internal counter for figuring out how many particles to launch per flow update.
233+
* @private
234+
*/
235+
this._flowQuantity = 0;
236+
237+
/**
238+
* @property {number} _flowTotal - Internal counter for figuring out how many particles to launch in total.
239+
* @private
240+
*/
241+
this._flowTotal = 0;
242+
231243
/**
232244
* @property {boolean} _explode - Internal helper for the style of particle emission (all at once, or one at a time).
233245
* @private
@@ -256,15 +268,50 @@ Phaser.Particles.Arcade.Emitter.prototype.update = function () {
256268
{
257269
this._timer = this.game.time.time + this.frequency * this.game.time.slowMotion;
258270

259-
if (this.emitParticle())
271+
if (this._flowTotal !== 0)
260272
{
261-
this._counter++;
262-
263-
if (this._quantity > 0 && this._counter >= this._quantity)
273+
if (this._flowQuantity > 0)
264274
{
265-
this.on = false;
275+
for (var i = 0; i < this._flowQuantity; i++)
276+
{
277+
if (this.emitParticle())
278+
{
279+
this._counter++;
280+
281+
if (this._flowTotal !== -1 && this._counter >= this._flowTotal)
282+
{
283+
this.on = false;
284+
break;
285+
}
286+
}
287+
}
288+
}
289+
else
290+
{
291+
if (this.emitParticle())
292+
{
293+
this._counter++;
294+
295+
if (this._flowTotal !== -1 && this._counter >= this._flowTotal)
296+
{
297+
this.on = false;
298+
}
299+
}
266300
}
267301
}
302+
else
303+
{
304+
if (this.emitParticle())
305+
{
306+
this._counter++;
307+
308+
if (this._quantity > 0 && this._counter >= this._quantity)
309+
{
310+
this.on = false;
311+
}
312+
}
313+
}
314+
268315
}
269316

270317
var i = this.children.length;
@@ -385,21 +432,53 @@ Phaser.Particles.Arcade.Emitter.prototype.revive = function () {
385432
*/
386433
Phaser.Particles.Arcade.Emitter.prototype.explode = function (lifespan, quantity) {
387434

435+
this._flowTotal = 0;
436+
388437
this.start(true, lifespan, 0, quantity, false);
389438

390439
};
391440

392441
/**
393442
* Call this function to start emitting a flow of particles at the given frequency.
443+
* It will carry on going until the total given is reached.
444+
* Each time the flow is run the quantity number of particles will be emitted together.
445+
* If you set the total to be 20 and quantity to be 5 then flow will emit 4 times in total (4 x 5 = 20 total)
446+
* If you set the total to be -1 then no quantity cap is used and it will keep emitting.
394447
*
395448
* @method Phaser.Particles.Arcade.Emitter#flow
396449
* @param {number} [lifespan=0] - How long each particle lives once emitted in ms. 0 = forever.
397-
* @param {number} [frequency=250] - Frequency is how often to emit a particle, given in ms.
398-
* @param {number} [quantity=0] - How many particles to launch.
450+
* @param {number} [frequency=250] - Frequency is how often to emit the particles, given in ms.
451+
* @param {number} [quantity=1] - How many particles to launch each time the frequency is met. Can never be > Emitter.maxParticles.
452+
* @param {number} [total=-1] - How many particles to launch in total. If -1 it will carry on indefinitely.
453+
* @param {boolean} [immediate=true] - Should the flow start immediately (true) or wait until the first frequency event? (false)
399454
*/
400-
Phaser.Particles.Arcade.Emitter.prototype.flow = function (lifespan, frequency, quantity) {
455+
Phaser.Particles.Arcade.Emitter.prototype.flow = function (lifespan, frequency, quantity, total, immediate) {
456+
457+
if (typeof quantity === 'undefined' || quantity === 0) { quantity = 1; }
458+
if (typeof total === 'undefined') { total = -1; }
459+
if (typeof immediate === 'undefined') { immediate = true; }
460+
461+
if (quantity > this.maxParticles)
462+
{
463+
quantity = this.maxParticles;
464+
}
465+
466+
this._counter = 0;
467+
this._flowQuantity = quantity;
468+
this._flowTotal = total;
469+
470+
if (immediate)
471+
{
472+
this.start(true, lifespan, frequency, quantity);
401473

402-
this.start(false, lifespan, frequency, quantity, true);
474+
this._counter += quantity;
475+
this.on = true;
476+
this._timer = this.game.time.time + frequency * this.game.time.slowMotion;
477+
}
478+
else
479+
{
480+
this.start(false, lifespan, frequency, quantity);
481+
}
403482

404483
};
405484

0 commit comments

Comments
 (0)