diff --git a/README.md b/README.md index d0f53b13a..b125dcb7e 100644 --- a/README.md +++ b/README.md @@ -258,11 +258,14 @@ If you are an exceptional JavaScript developer and would like to join the Phaser ### New Features +* 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. + ### Updates -* TypeScript definitions fixes and updates (thanks @clark-stevenson @milkey-mouse) +* TypeScript definitions fixes and updates (thanks @clark-stevenson @milkey-mouse @timotei @qdrj) * JSDoc typo fixes (thanks @rwrountree) * Math.average has been optimized (thanks @rwrountree #2025) +* Change splice.call(arguments, ..) to use slice instead (thanks @pnstickne #2034 #2032) ### Bug Fixes diff --git a/src/particles/arcade/Emitter.js b/src/particles/arcade/Emitter.js index 77daf8397..68194c59d 100644 --- a/src/particles/arcade/Emitter.js +++ b/src/particles/arcade/Emitter.js @@ -536,12 +536,23 @@ Phaser.Particles.Arcade.Emitter.prototype.start = function (explode, lifespan, f }; /** -* This function can be used both internally and externally to emit the next particle in the queue. +* This function is used internally to emit the next particle in the queue. +* +* However it can also be called externally to emit a particle. +* +* When called externally you can use the arguments to override any defaults the Emitter has set. * * @method Phaser.Particles.Arcade.Emitter#emitParticle +* @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`. +* @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`. +* @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. +* @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. * @return {boolean} True if a particle was emitted, otherwise false. */ -Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function () { +Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function (x, y, key, frame) { + + if (x === undefined) { x = null; } + if (y === undefined) { y = null; } var particle = this.getFirstExists(false); @@ -550,15 +561,41 @@ Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function () { return false; } - if (this.width > 1 || this.height > 1) + if (key !== undefined && frame !== undefined) { - particle.reset(this.game.rnd.integerInRange(this.left, this.right), this.game.rnd.integerInRange(this.top, this.bottom)); + particle.loadTexture(key, frame); } - else + else if (key !== undefined) { - particle.reset(this.emitX, this.emitY); + particle.loadTexture(key); } + var rndInt = this.game.rnd.between; + var rndFloat = this.game.rnd.realInRange; + + var emitX = this.emitX; + var emitY = this.emitY; + + if (x !== null) + { + emitX = x; + } + else if (this.width > 1) + { + emitX = rndInt(this.left, this.right); + } + + if (y !== null) + { + emitY = y; + } + else if (this.height > 1) + { + emitY = rndInt(this.top, this.bottom); + } + + particle.reset(this.emitX, this.emitY); + particle.angle = 0; particle.lifespan = this.lifespan; @@ -577,20 +614,23 @@ Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function () { } else if (this.minParticleScale !== 1 || this.maxParticleScale !== 1) { - particle.scale.set(this.game.rnd.realInRange(this.minParticleScale, this.maxParticleScale)); + particle.scale.set(rndFloat(this.minParticleScale, this.maxParticleScale)); } else if ((this._minParticleScale.x !== this._maxParticleScale.x) || (this._minParticleScale.y !== this._maxParticleScale.y)) { - particle.scale.set(this.game.rnd.realInRange(this._minParticleScale.x, this._maxParticleScale.x), this.game.rnd.realInRange(this._minParticleScale.y, this._maxParticleScale.y)); + particle.scale.set(rndFloat(this._minParticleScale.x, this._maxParticleScale.x), rndFloat(this._minParticleScale.y, this._maxParticleScale.y)); } - if (Array.isArray(this._frames === 'object')) + if (frame === undefined) { - particle.frame = this.game.rnd.pick(this._frames); - } - else - { - particle.frame = this._frames; + if (Array.isArray(this._frames)) + { + particle.frame = this.game.rnd.pick(this._frames); + } + else + { + particle.frame = this._frames; + } } if (this.autoAlpha) @@ -599,25 +639,29 @@ Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function () { } else { - particle.alpha = this.game.rnd.realInRange(this.minParticleAlpha, this.maxParticleAlpha); + particle.alpha = rndFloat(this.minParticleAlpha, this.maxParticleAlpha); } particle.blendMode = this.blendMode; - particle.body.updateBounds(); + var body = particle.body; - particle.body.bounce.setTo(this.bounce.x, this.bounce.y); + body.updateBounds(); - particle.body.velocity.x = this.game.rnd.between(this.minParticleSpeed.x, this.maxParticleSpeed.x); - particle.body.velocity.y = this.game.rnd.between(this.minParticleSpeed.y, this.maxParticleSpeed.y); - particle.body.angularVelocity = this.game.rnd.between(this.minRotation, this.maxRotation); + // body.bounce.setTo(this.bounce.x, this.bounce.y); + body.bounce.copyFrom(this.bounce); - particle.body.gravity.y = this.gravity; + body.velocity.x = rndInt(this.minParticleSpeed.x, this.maxParticleSpeed.x); + body.velocity.y = rndInt(this.minParticleSpeed.y, this.maxParticleSpeed.y); + body.angularVelocity = rndInt(this.minRotation, this.maxRotation); - particle.body.drag.x = this.particleDrag.x; - particle.body.drag.y = this.particleDrag.y; + body.gravity.y = this.gravity; - particle.body.angularDrag = this.angularDrag; + body.drag.copyFrom(this.particleDrag); + // body.drag.x = this.particleDrag.x; + // body.drag.y = this.particleDrag.y; + + body.angularDrag = this.angularDrag; particle.onEmit();