HTML and CSS Reference
In-Depth Information
Pooling Object Instances
We have looked at object pools as they relate to sounds, but we have not applied this concept
to our game objects. Object pooling is a technique designed to save processing time, so it is
very applicable to an arcade game application such as the one we are building. By pooling
object instances, weavoid the sometimes processor-intensive task ofcreating object instances
on the fly during game execution. This is especially applicable to our particle explosions be-
cause we create multiple objects on the same frame tick. On a lower-powered platform, such
as a handheld device, object pooling can help increase frame rate.
Object pooling in Geo Blaster Extended
Inourgame,weapplythepoolingconcepttotheexplosionparticles.Ofcourse,wecanextend
this concept to rocks, projectiles, saucers, and any other type of object that requires multiple
instances. For this example, though, let's focus on the particles. As we will see, adding pool-
ing in JavaScript is a relatively simple but powerful technique.
Adding pooling variables to our game
We need to add four application scope variables to our game to use pooling for our game
particle:
var
var particlePool = [];
var
var maxParticles = 200 ;
var
var newParticle ;
var
var tempParticle ;
The particlePool array holds the list of particle object instances that are waiting to be
used. When createExplode() needs to use a particle, it first sees whether any are available
in this array. If one is available, it is popped off the top of the particlePool stack and placed
in the application scope newParticle variable—which is a reference to the pooled particle.
The createExplode() function sets the properties of the newParticle and then pushes it to
the end of the existing particles array.
When a particle's life has been exhausted, the updateParticles() function splices the
particle from the particles array and pushes it back into the particlePool array. We have
created the tempParticle reference to alleviate the updateParticles() function's need to
create this instance on each frame tick.
The maxParticles value is used in a new function called createObjectPools() . We call
this function in the gameStateInit() state function before we create the sound and tile sheet
loading events.
Search WWH ::




Custom Search