HTML and CSS Reference
In-Depth Information
than
0
. If a particle is available, it will be added to the
particles
array after its attributes
are set. If no particle is available, none will be used.
This functionality can be extended to add a particle as needed to the
pool when none is available. We have not added that functionality to
our example, but it is common in some pooling algorithms.
Here is the newly modified
createExplode()
function in its entirety:
function createExplode(x,y,num,type) {
playSound(SOUND_EXPLODE,.5);
for (var partCtr=0;partCtr<num;partCtr++){
if (particlePool.length > 0){
newParticle = particlePool.pop();
newParticle.dx = Math.random()*3;
if (Math.random()<.5){
newParticle.dx* = -1;
}
newParticle.dy = Math.random()*3;
if (Math.random()<.5){
newParticle.dy* = -1;
}
newParticle.life = Math.floor(Math.random()*30+30);
newParticle.lifeCtr = 0;
newParticle.x = x;
newParticle.width = 2;
newParticle.height = 2;
newParticle.y = y;
newParticle.type = type;
//ConsoleLog.log("newParticle.life=" + newParticle.life);
particles.push(newParticle);
}
}
}
The
updateParticles()
function will loop through the
particles
instances, update the
attributes of each, and then check to see whether the particle's life has been exhausted.
If it has, the function will place the particle back in the pool. Here is the code we will
add to
updateParticles()
to replenish the pool:
if (remove) {
particlePool.push(tempParticle)
particles.splice(particleCtr,1)
}