<ahref="global.html#Phaser.Path#numPointsreturn%257Bnumber%257DThetotalnumberofPathPointsinthisPath.">Phaser.Path#numPoints
return {number} The total number of PathPoints in this Path.</a>
* Emitter is a lightweight particle emitter that uses Arcade Physics.
* It can be used for one-time explosions or for continuous effects like rain and fire.
* All it really does is launch Particle objects out at set intervals, and fixes their positions and velocities accordingly.
*
* @class Phaser.Particles.Arcade.Emitter
* @constructor
* @extends Phaser.Group
* @param {Phaser.Game} game - Current game instance.
* @param {number} [x=0] - The x coordinate within the Emitter that the particles are emitted from.
* @param {number} [y=0] - The y coordinate within the Emitter that the particles are emitted from.
* @param {number} [maxParticles=50] - The total number of particles in this emitter.
*/
Phaser.Particles.Arcade.Emitter = function (game, x, y, maxParticles) {
/**
* @property {number} maxParticles - The total number of particles in this emitter.
* @default
*/
this.maxParticles = maxParticles || 50;
Phaser.Group.call(this, game);
/**
* @property {number} _id - Internal ID for this emitter -- only used by the Particle System in most cases
* @private
*/
this._id = this.game.particles.ID++;
/**
* @property {string} name - A handy string name for this emitter. Can be set to anything.
*/
this.name = 'emitter' + this.id;
/**
* @property {number} type - Internal Phaser Type value.
* @protected
*/
this.type = Phaser.EMITTER;
/**
* @property {number} physicsType - The const physics body type of this object.
* @readonly
*/
this.physicsType = Phaser.GROUP;
/**
* @property {Phaser.Rectangle} area - The area of the emitter. Particles can be randomly generated from anywhere within this rectangle.
* @default
*/
this.area = new Phaser.Rectangle(x, y, 1, 1);
/**
* @property {Phaser.Point} minParticleSpeed - The minimum possible velocity of a particle.
* @default
*/
this.minParticleSpeed = new Phaser.Point(-100, -100);
/**
* @property {Phaser.Point} maxParticleSpeed - The maximum possible velocity of a particle.
* @default
*/
this.maxParticleSpeed = new Phaser.Point(100, 100);
/**
* @property {number} minParticleScale - The minimum possible scale of a particle. This is applied to the X and Y axis. If you need to control each axis see minParticleScaleX.
* @default
*/
this.minParticleScale = 1;
/**
* @property {number} maxParticleScale - The maximum possible scale of a particle. This is applied to the X and Y axis. If you need to control each axis see maxParticleScaleX.
* @default
*/
this.maxParticleScale = 1;
/**
* @property {array} scaleData - An array of the calculated scale easing data applied to particles with scaleRates > 0.
*/
this.scaleData = null;
/**
* @property {number} minRotation - The minimum possible angular velocity of a particle.
* @default
*/
this.minRotation = -360;
/**
* @property {number} maxRotation - The maximum possible angular velocity of a particle.
* @default
*/
this.maxRotation = 360;
/**
* @property {number} minParticleAlpha - The minimum possible alpha value of a particle.
* @default
*/
this.minParticleAlpha = 1;
/**
* @property {number} maxParticleAlpha - The maximum possible alpha value of a particle.
* @default
*/
this.maxParticleAlpha = 1;
/**
* @property {array} alphaData - An array of the calculated alpha easing data applied to particles with alphaRates > 0.
*/
this.alphaData = null;
/**
* @property {Phaser.Point} gravity - Sets the `body.gravity` of each particle sprite to this on launch.
* @default
*/
this.gravity = new Phaser.Point(0, 100);
/**
* @property {any} particleClass - For emitting your own particle class types. They must extend Phaser.Particle.
* @default
*/
this.particleClass = Phaser.Particle;
/**
* @property {Phaser.Point} particleDrag - The X and Y drag component of particles launched from the emitter.
*/
this.particleDrag = new Phaser.Point();
/**
* @property {number} angularDrag - The angular drag component of particles launched from the emitter if they are rotating.
* @default
*/
this.angularDrag = 0;
/**
* @property {number} frequency - How often a particle is emitted in ms (if emitter is started with Explode === false).
* @default
*/
this.frequency = 100;
/**
* @property {number} lifespan - How long each particle lives once it is emitted in ms. Default is 2 seconds. Set lifespan to 'zero' for particles to live forever.
* @default
*/
this.lifespan = 2000;
/**
* @property {Phaser.Point} bounce - How much each particle should bounce on each axis. 1 = full bounce, 0 = no bounce.
*/
this.bounce = new Phaser.Point();
/**
* @property {boolean} on - Determines whether the emitter is currently emitting particles. It is totally safe to directly toggle this.
* @default
*/
this.on = false;
/**
* @property {Phaser.Point} particleAnchor - When a particle is created its anchor will be set to match this Point object (defaults to x/y: 0.5 to aid in rotation)
* @default
*/
this.particleAnchor = new Phaser.Point(0.5, 0.5);
/**
* @property {number} blendMode - The blendMode as set on the particle when emitted from the Emitter. Defaults to NORMAL. Needs browser capable of supporting canvas blend-modes (most not available in WebGL)
* @default
*/
this.blendMode = Phaser.blendModes.NORMAL;
/**
* The point the particles are emitted from.
* Emitter.x and Emitter.y control the containers location, which updates all current particles
* Emitter.emitX and Emitter.emitY control the emission location relative to the x/y position.
* @property {number} emitX
*/
this.emitX = x;
/**
* The point the particles are emitted from.
* Emitter.x and Emitter.y control the containers location, which updates all current particles
* Emitter.emitX and Emitter.emitY control the emission location relative to the x/y position.
* @property {number} emitY
*/
this.emitY = y;
/**
* @property {boolean} autoScale - When a new Particle is emitted this controls if it will automatically scale in size. Use Emitter.setScale to configure.
*/
this.autoScale = false;
/**
* @property {boolean} autoAlpha - When a new Particle is emitted this controls if it will automatically change alpha. Use Emitter.setAlpha to configure.
*/
this.autoAlpha = false;
/**
* @property {boolean} particleBringToTop - If this is `true` then when the Particle is emitted it will be bought to the top of the Emitters display list.
* @default
*/
this.particleBringToTop = false;
/**
* @property {boolean} particleSendToBack - If this is `true` then when the Particle is emitted it will be sent to the back of the Emitters display list.
* @default
*/
this.particleSendToBack = false;
/**
* @property {Phaser.Point} _minParticleScale - Internal particle scale var.
* @private
*/
this._minParticleScale = new Phaser.Point(1, 1);
/**
* @property {Phaser.Point} _maxParticleScale - Internal particle scale var.
* @private
*/
this._maxParticleScale = new Phaser.Point(1, 1);
/**
* @property {number} _quantity - Internal helper for deciding how many particles to launch.
* @private
*/
this._quantity = 0;
/**
* @property {number} _timer - Internal helper for deciding when to launch particles or kill them.
* @private
*/
this._timer = 0;
/**
* @property {number} _counter - Internal counter for figuring out how many particles to launch.
* @private
*/
this._counter = 0;
/**
* @property {number} _flowQuantity - Internal counter for figuring out how many particles to launch per flow update.
* @private
*/
this._flowQuantity = 0;
/**
* @property {number} _flowTotal - Internal counter for figuring out how many particles to launch in total.
* @private
*/
this._flowTotal = 0;
/**
* @property {boolean} _explode - Internal helper for the style of particle emission (all at once, or one at a time).
* @private
*/
this._explode = true;
/**
* @property {any} _frames - Internal helper for the particle frame.
* @param {array|string} keys - A string or an array of strings that the particle sprites will use as their texture. If an array one is picked at random.
* @param {array|number} [frames=0] - A frame number, or array of frames that the sprite will use. If an array one is picked at random.
* @param {number} [quantity] - The number of particles to generate. If not given it will use the value of Emitter.maxParticles. If the value is greater than Emitter.maxParticles it will use Emitter.maxParticles as the quantity.
* @param {boolean} [collide=false] - If you want the particles to be able to collide with other Arcade Physics bodies then set this to true.
* @param {boolean} [collideWorldBounds=false] - A particle can be set to collide against the World bounds automatically and rebound back into the World if this is set to true. Otherwise it will leave the World.
* @return {Phaser.Particles.Arcade.Emitter} This Emitter instance.
*/
Phaser.Particles.Arcade.Emitter.prototype.makeParticles = function (keys, frames, quantity, collide, collideWorldBounds) {
if (frames === undefined) { frames = 0; }
if (quantity === undefined) { quantity = this.maxParticles; }
if (collide === undefined) { collide = false; }
if (collideWorldBounds === undefined) { collideWorldBounds = false; }
var particle;
var i = 0;
var rndKey = keys;
var rndFrame = frames;
this._frames = frames;
if (quantity > this.maxParticles)
{
this.maxParticles = quantity;
}
while (i < quantity)
{
if (Array.isArray(keys))
{
rndKey = this.game.rnd.pick(keys);
}
if (Array.isArray(frames))
{
rndFrame = this.game.rnd.pick(frames);
}
particle = new this.particleClass(this.game, 0, 0, rndKey, rndFrame);
if (immediate === undefined) { immediate = true; }
if (quantity > this.maxParticles)
{
quantity = this.maxParticles;
}
this._counter = 0;
this._flowQuantity = quantity;
this._flowTotal = total;
if (immediate)
{
this.start(true, lifespan, frequency, quantity);
this._counter += quantity;
this.on = true;
this._timer = this.game.time.time + frequency * this.game.time.slowMotion;
}
else
{
this.start(false, lifespan, frequency, quantity);
}
return this;
};
/**
* Call this function to start emitting particles.
*
* @method Phaser.Particles.Arcade.Emitter#start
* @param {boolean} [explode=true] - Whether the particles should all burst out at once (true) or at the frequency given (false).
* @param {number} [lifespan=0] - How long each particle lives once emitted in ms. 0 = forever.
* @param {number} [frequency=250] - Ignored if Explode is set to true. Frequency is how often to emit 1 particle. Value given in ms.
* @param {number} [quantity=0] - How many particles to launch. 0 = "all of the particles" which will keep emitting until Emitter.maxParticles is reached.
* @param {number} [forceQuantity=false] - If `true` and creating a particle flow, the quantity emitted will be forced to the be quantity given in this call. This can never exceed Emitter.maxParticles.
* @return {Phaser.Particles.Arcade.Emitter} This Emitter instance.
*/
Phaser.Particles.Arcade.Emitter.prototype.start = function (explode, lifespan, frequency, quantity, forceQuantity) {
if (explode === undefined) { explode = true; }
if (lifespan === undefined) { lifespan = 0; }
if (frequency === undefined || frequency === null) { frequency = 250; }
if (quantity === undefined) { quantity = 0; }
if (forceQuantity === undefined) { forceQuantity = false; }
if (quantity > this.maxParticles)
{
quantity = this.maxParticles;
}
this.revive();
this.visible = true;
this.lifespan = lifespan;
this.frequency = frequency;
if (explode || forceQuantity)
{
for (var i = 0; i < quantity; i++)
{
this.emitParticle();
}
}
else
{
this.on = true;
this._quantity = quantity;
this._counter = 0;
this._timer = this.game.time.time + frequency * this.game.time.slowMotion;
}
return this;
};
/**
* 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.
* @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 (x, y, key, frame) {
if (x === undefined) { x = null; }
if (y === undefined) { y = null; }
var particle = this.getFirstExists(false);
if (particle === null)
{
return false;
}
var rnd = this.game.rnd;
if (key !== undefined && frame !== undefined)
{
particle.loadTexture(key, frame);
}
else if (key !== undefined)
{
particle.loadTexture(key);
}
var emitX = this.emitX;
var emitY = this.emitY;
if (x !== null)
{
emitX = x;
}
else if (this.width > 1)
{
emitX = rnd.between(this.left, this.right);
}
if (y !== null)
{
emitY = y;
}
else if (this.height > 1)
{
emitY = rnd.between(this.top, this.bottom);
}
particle.reset(emitX, emitY);
particle.angle = 0;
particle.lifespan = this.lifespan;
if (this.particleBringToTop)
{
this.bringToTop(particle);
}
else if (this.particleSendToBack)
{
this.sendToBack(particle);
}
if (this.autoScale)
{
particle.setScaleData(this.scaleData);
}
else if (this.minParticleScale !== 1 || this.maxParticleScale !== 1)
* @param {number} [min=1] - The minimum value for this range.
* @param {number} [max=1] - The maximum value for this range.
* @param {number} [rate=0] - The rate (in ms) at which the particles will change in alpha from min to max, or set to zero to pick a random alpha between the two.
* @param {function} [ease=Phaser.Easing.Linear.None] - If you've set a rate > 0 this is the easing formula applied between the min and max values.
* @param {boolean} [yoyo=false] - If you've set a rate > 0 you can set if the ease will yoyo or not (i.e. ease back to its original values)
* @return {Phaser.Particles.Arcade.Emitter} This Emitter instance.
*/
Phaser.Particles.Arcade.Emitter.prototype.setAlpha = function (min, max, rate, ease, yoyo) {
if (min === undefined) { min = 1; }
if (max === undefined) { max = 1; }
if (rate === undefined) { rate = 0; }
if (ease === undefined) { ease = Phaser.Easing.Linear.None; }
if (yoyo === undefined) { yoyo = false; }
this.minParticleAlpha = min;
this.maxParticleAlpha = max;
this.autoAlpha = false;
if (rate > 0 && min !== max)
{
var tweenData = { v: min };
var tween = this.game.make.tween(tweenData).to( { v: max }, rate, ease);
tween.yoyo(yoyo);
this.alphaData = tween.generateData(60);
// Inverse it so we don't have to do array length look-ups in Particle update loops
this.alphaData.reverse();
this.autoAlpha = true;
}
return this;
};
/**
* A more compact way of setting the scale constraints of the particles.
* The rate parameter, if set to a value above zero, lets you set the speed and ease which the Particle uses to change in scale from min to max across both axis.
* If rate is zero, which is the default, the particle won't change scale during update, instead it will pick a random scale between min and max on emit.
* @param {number} [minX=1] - The minimum value of Particle.scale.x.
* @param {number} [maxX=1] - The maximum value of Particle.scale.x.
* @param {number} [minY=1] - The minimum value of Particle.scale.y.
* @param {number} [maxY=1] - The maximum value of Particle.scale.y.
* @param {number} [rate=0] - The rate (in ms) at which the particles will change in scale from min to max, or set to zero to pick a random size between the two.
* @param {function} [ease=Phaser.Easing.Linear.None] - If you've set a rate > 0 this is the easing formula applied between the min and max values.
* @param {boolean} [yoyo=false] - If you've set a rate > 0 you can set if the ease will yoyo or not (i.e. ease back to its original values)
* @return {Phaser.Particles.Arcade.Emitter} This Emitter instance.
// Inverse it so we don't have to do array length look-ups in Particle update loops
this.scaleData.reverse();
this.autoScale = true;
}
return this;
};
/**
* Change the emitters center to match the center of any object with a `center` property, such as a Sprite.
* If the object doesn't have a center property it will be set to object.x + object.width / 2
*
* @method Phaser.Particles.Arcade.Emitter#at
* @param {object|Phaser.Sprite|Phaser.Image|Phaser.TileSprite|Phaser.Text|PIXI.DisplayObject} object - The object that you wish to match the center with.
* @return {Phaser.Particles.Arcade.Emitter} This Emitter instance.
*/
Phaser.Particles.Arcade.Emitter.prototype.at = function (object) {