Game Development Reference
In-Depth Information
var randInt:int = int(Math.random() * 359);
tempParticle.dx = rotationVectorList[randInt].x;
tempParticle.dy = rotationVectorList[randInt].y;
particleManager.particles.push(tempParticle);
}
}
}
The function loops though the passed-in parts number to create an explosion that starts at the
xval and yval passed in coordinates as the center. Particles will randomly choose a direction to
move and then shoot out from this point.
First, the createExplode function checks to make sure that the pool contains a Particle instance
for use. If there is no Particle left in the pool, no Particle is created. If possible, it takes a
Particle from the pool, changes its properties and adds it to the particles array. It does so by
assigning the tempParticle variable a reference to the last particle in the particlePool array by
popping the next particle from the particlePool array and setting the tempParticle to reference
it. After the attributes of tempParticle are set, it is placed in the particles array.
Making a particle inactive
When a particle has used up its life, it will be moved from the particles array back to the
particlePool array. This occurs in the update function of the BlasterMines.as class:
particleManager.particleCount = particleManager.particles.length-1
for (var ctr:int = particleManager.particleCount; ctr >= 0; ctr--) {
tempParticle = particleManager.particles[ctr];
if (tempParticle.update(step)) { //return true if particle is to be removed
tempParticle.frame = 0;
particleManager.particlePool.push(tempParticle);
particleManager.particles.splice(ctr,1);
}
}
Optimizing with single-unit processing and
memory conservation
Sometimes, we call the concept of optimizing with single-unit processing “a pool of one.”
Throughout this topic, we have created numerous class-level variables that are reused for
processing. By “class-level,” we mean a variable with global scope that every function in the
class can use. We tend to do this for objects such as Point and Rectangle instances, because
they are created and used a lot (30 to 40 times a second for each blitted object, for instance).
By creating a single one of each to use, we are essentially creating a pool of one. This
technique is a somewhat controversial, because (as with the object pools) by saving the
processor time in creating these objects, we are also using up more memory, or creating a
larger memory footprint, when our game initializes. The same theory from the pool section
holds here though. Overall, we are reducing memory consumption by creating these “pools of
Search WWH ::




Custom Search