Jump to content

Questions about emitter.


Recommended Posts

Hello there!

I , for the first time in my life, tried to understand what an emitter is . I think i understood it a bit. But since i got no experience with this i ran into some problems.


I'm creating a shooting game and i want some explosions. Specifically when an enemy or the player dies. 

Same textures for each explosion.


Firstly i create the emitter:

this.emitter = game.add.emitter(300,200, 100);this.emitter.makeParticles(['explosion0','explosion1','explosion2']);this.emitter.gravity=0;this.emitter.lifespan=2000;this.emitter.minRotation = 0;this.emitter.setAlpha(0.1, 0.1)this.emitter.maxRotation = 0;this.emitter.maxParticleSpeed=1;

then, i create the same block of code for the player and the enemies and call it when one of them dies.

this.state.emitter.x = enemy.x;this.state.emitter.y = enemy.y;this.state.emitter.start(false, 500, 40, 10);

The first problem is, when i kill an enemy, the emitter starts and works correctly. But when i kill another enemy before the spawning of the particles end it will stop the explosion of the first enemy and it will start the other. The same happens if the player dies after an enemy. It stop the enemy's explosion to start the player's.

I'm guessing the solution here is to create multiple emitter. Or is there a better way?


The second problem is that after each explosion, the time it takes for each explosion to stop takes longer. It starts ,lets say, from 1 second till the explosion is done and it can reach a point where it takes 10+ seconds to finish.

I can't  find what i'm doing wrong , since i don't really understand how an emitter works.


If playing the game helps, i can give you a link of it running.


Thank you in advance!

Link to comment
Share on other sites

You're correct - the way to do this would be to create multiple emitters, since you're not using the 'explosion' flag. If first argument in .start() is true, the emitter will send out all of its particles at once, meaning you could get away with only using one emitter for all the explosions, but as you're emitting a stream of particles for a period of time, there's the possibility of overlap.


As for your continually increasing emitter length, I'm not entirely sure about that one. It could be because of the overlapping calls to start, which add up maybe?


I'd create a pool of several emitters in an array, and each time you create an explosion do something like this (tweaking the amount of explosions created in the pool to suit the pace of your game and performance concerns):


Edit: Some small fixes to syntax.

var explosionPool = [];var currentExplosion = 0;var emitter;for (var e = 0; e < 10; e++) {  emitter = game.add.emitter(0, 0, 100);  /* ... your emitter setup here ... */  explosionPool.push(emitter);}function explode(enemy) {  // first get an explosion from the pool  var emitter = explosionPool[currentExplosion];  // place the emitter position at the enemy's position via Point.copyFrom  emitter.position.copyFrom(enemy.position);  emitter.start(false, 500, 40, 10);  // increase currentExplosion by 1, ensuring it wraps around back to 0  // when it reaches the length of the pool array  currentExplosion = Phaser.Math.wrap(currentExplosion + 1, 0, explosionPool.length);}
Edited by lewster32
Link to comment
Share on other sites

The wrap function is another way of doing this:

currentExplosion = (currentExplosion + 1) % explosionPool.length;

The modulo (%) operator can come in quite handy for cycling values. Phaser has some very handy stuff tucked away in its Point, Rectangle, Circle and Math classes that people often miss (including myself!)

Link to comment
Share on other sites

I was looking at the start() function of emitter, and i saw that when i pass explode=false and a quantity of particles, it adds up to the existing quantity.


You can see it on the line  here : http://docs.phaser.io/Emitter.js.html#sunlight-1-line-431


Is it intended?

If yes,shouldn't it somewhere cap the quantity since i entered a value for maximum particles on the creation of the emitter ?


Or maybe i don't understand something again  :P

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...