HTML and CSS Reference
In-Depth Information
In the demo, you loop over the pool objects array twice, first to move particles around, in the physics step,
then to render them. In the loops the particles are processed only if their allocated flag is set to true , so you ignore
unallocated particles instead of discarding them. In this way, you are holding references to the unallocated particles
rather than generating garbage, as you would if you were not using a pool (see listing 3-6).
Listing 3-6. Looping over Only Allocated Objects
for (i = 0; i < particles.elements.length; i++) {
var particle = particles.elements[i];
if( particle.allocated === true ){
if( particle.life === 0 )
particles.free( particle );
else
demo.integrate( particle );
particle.life--;
}
}
Using an Object Pool
As in real life, filling a pool takes time and can be expensive. You must avoid having the object pools fill while the
player is playing your game by preallocating the necessary number of objects during the initialization of the game,
effectively moving the cost of creating objects up front.
You may also prefer not to allow some of your pools to grow. For instance, you may want to have only a fixed
number of particles in your game. In this case, when you need to display a particle, and there is no free one left, you
can choose not to spawn a particle at all or to remove the oldest one to spawn it in a new place. Do you remember
trying to write your name with bullet holes in Half-Life, and the ones you shot first disappeared as you were writing?
Well, now you know what was happening.
During the development process, you should monitor your pool usages to determine the levels at which your
pools should initially be filled.
For more information on static memory JavaScript and object pools, you may want to consult HTML5Rocks.com
( www.html5rocks.com/en/tutorials/speed/static-mem-pools ).
 
Search WWH ::




Custom Search