Skip to content

Commit 9798864

Browse files
committed
Emitter.emitParticle now has 4 new optional arguments: x, y, key and frame. These allow you to override whatever the Emitter default values may be and emit the particle from the given coordinates and with a new texture.
1 parent f380549 commit 9798864

2 files changed

Lines changed: 72 additions & 25 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,14 @@ If you are an exceptional JavaScript developer and would like to join the Phaser
258258

259259
### New Features
260260

261+
* Emitter.emitParticle now has 4 new optional arguments: `x`, `y`, `key` and `frame`. These allow you to override whatever the Emitter default values may be and emit the particle from the given coordinates and with a new texture.
262+
261263
### Updates
262264

263-
* TypeScript definitions fixes and updates (thanks @clark-stevenson @milkey-mouse)
265+
* TypeScript definitions fixes and updates (thanks @clark-stevenson @milkey-mouse @timotei @qdrj)
264266
* JSDoc typo fixes (thanks @rwrountree)
265267
* Math.average has been optimized (thanks @rwrountree #2025)
268+
* Change splice.call(arguments, ..) to use slice instead (thanks @pnstickne #2034 #2032)
266269

267270
### Bug Fixes
268271

src/particles/arcade/Emitter.js

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -536,12 +536,23 @@ Phaser.Particles.Arcade.Emitter.prototype.start = function (explode, lifespan, f
536536
};
537537

538538
/**
539-
* This function can be used both internally and externally to emit the next particle in the queue.
539+
* This function is used internally to emit the next particle in the queue.
540+
*
541+
* However it can also be called externally to emit a particle.
542+
*
543+
* When called externally you can use the arguments to override any defaults the Emitter has set.
540544
*
541545
* @method Phaser.Particles.Arcade.Emitter#emitParticle
546+
* @param {number} [x] - The x coordinate to emit the particle from. If `null` or `undefined` it will use `Emitter.emitX` or if the Emitter has a width > 1 a random value between `Emitter.left` and `Emitter.right`.
547+
* @param {number} [y] - The y coordinate to emit the particle from. If `null` or `undefined` it will use `Emitter.emitY` or if the Emitter has a height > 1 a random value between `Emitter.top` and `Emitter.bottom`.
548+
* @param {string|Phaser.RenderTexture|Phaser.BitmapData|Phaser.Video|PIXI.Texture} [key] - This is the image or texture used by the Particle 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.
549+
* @param {string|number} [frame] - If this Particle 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.
542550
* @return {boolean} True if a particle was emitted, otherwise false.
543551
*/
544-
Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function () {
552+
Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function (x, y, key, frame) {
553+
554+
if (x === undefined) { x = null; }
555+
if (y === undefined) { y = null; }
545556

546557
var particle = this.getFirstExists(false);
547558

@@ -550,15 +561,41 @@ Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function () {
550561
return false;
551562
}
552563

553-
if (this.width > 1 || this.height > 1)
564+
if (key !== undefined && frame !== undefined)
554565
{
555-
particle.reset(this.game.rnd.integerInRange(this.left, this.right), this.game.rnd.integerInRange(this.top, this.bottom));
566+
particle.loadTexture(key, frame);
556567
}
557-
else
568+
else if (key !== undefined)
569+
{
570+
particle.loadTexture(key);
571+
}
572+
573+
var rndInt = this.game.rnd.between;
574+
var rndFloat = this.game.rnd.realInRange;
575+
576+
var emitX = this.emitX;
577+
var emitY = this.emitY;
578+
579+
if (x !== null)
580+
{
581+
emitX = x;
582+
}
583+
else if (this.width > 1)
584+
{
585+
emitX = rndInt(this.left, this.right);
586+
}
587+
588+
if (y !== null)
589+
{
590+
emitY = y;
591+
}
592+
else if (this.height > 1)
558593
{
559-
particle.reset(this.emitX, this.emitY);
594+
emitY = rndInt(this.top, this.bottom);
560595
}
561596

597+
particle.reset(this.emitX, this.emitY);
598+
562599
particle.angle = 0;
563600
particle.lifespan = this.lifespan;
564601

@@ -577,20 +614,23 @@ Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function () {
577614
}
578615
else if (this.minParticleScale !== 1 || this.maxParticleScale !== 1)
579616
{
580-
particle.scale.set(this.game.rnd.realInRange(this.minParticleScale, this.maxParticleScale));
617+
particle.scale.set(rndFloat(this.minParticleScale, this.maxParticleScale));
581618
}
582619
else if ((this._minParticleScale.x !== this._maxParticleScale.x) || (this._minParticleScale.y !== this._maxParticleScale.y))
583620
{
584-
particle.scale.set(this.game.rnd.realInRange(this._minParticleScale.x, this._maxParticleScale.x), this.game.rnd.realInRange(this._minParticleScale.y, this._maxParticleScale.y));
621+
particle.scale.set(rndFloat(this._minParticleScale.x, this._maxParticleScale.x), rndFloat(this._minParticleScale.y, this._maxParticleScale.y));
585622
}
586623

587-
if (Array.isArray(this._frames === 'object'))
588-
{
589-
particle.frame = this.game.rnd.pick(this._frames);
590-
}
591-
else
624+
if (frame === undefined)
592625
{
593-
particle.frame = this._frames;
626+
if (Array.isArray(this._frames))
627+
{
628+
particle.frame = this.game.rnd.pick(this._frames);
629+
}
630+
else
631+
{
632+
particle.frame = this._frames;
633+
}
594634
}
595635

596636
if (this.autoAlpha)
@@ -599,25 +639,29 @@ Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function () {
599639
}
600640
else
601641
{
602-
particle.alpha = this.game.rnd.realInRange(this.minParticleAlpha, this.maxParticleAlpha);
642+
particle.alpha = rndFloat(this.minParticleAlpha, this.maxParticleAlpha);
603643
}
604644

605645
particle.blendMode = this.blendMode;
606646

607-
particle.body.updateBounds();
647+
var body = particle.body;
648+
649+
body.updateBounds();
608650

609-
particle.body.bounce.setTo(this.bounce.x, this.bounce.y);
651+
// body.bounce.setTo(this.bounce.x, this.bounce.y);
652+
body.bounce.copyFrom(this.bounce);
610653

611-
particle.body.velocity.x = this.game.rnd.between(this.minParticleSpeed.x, this.maxParticleSpeed.x);
612-
particle.body.velocity.y = this.game.rnd.between(this.minParticleSpeed.y, this.maxParticleSpeed.y);
613-
particle.body.angularVelocity = this.game.rnd.between(this.minRotation, this.maxRotation);
654+
body.velocity.x = rndInt(this.minParticleSpeed.x, this.maxParticleSpeed.x);
655+
body.velocity.y = rndInt(this.minParticleSpeed.y, this.maxParticleSpeed.y);
656+
body.angularVelocity = rndInt(this.minRotation, this.maxRotation);
614657

615-
particle.body.gravity.y = this.gravity;
658+
body.gravity.y = this.gravity;
616659

617-
particle.body.drag.x = this.particleDrag.x;
618-
particle.body.drag.y = this.particleDrag.y;
660+
body.drag.copyFrom(this.particleDrag);
661+
// body.drag.x = this.particleDrag.x;
662+
// body.drag.y = this.particleDrag.y;
619663

620-
particle.body.angularDrag = this.angularDrag;
664+
body.angularDrag = this.angularDrag;
621665

622666
particle.onEmit();
623667

0 commit comments

Comments
 (0)