Game Development Reference
In-Depth Information
object pools will be filled and ready for use. The way we conserve memory with this technique is
twofold and subtle.
First, if we set the pool size to a limited amount, we stop the system from creating too many
objects and using up all of the available memory. This can be handled other ways such as setting
maximum values variables that are checked when objects need to be created, but it works for the
pool also.
Second, the pool of objects helps smooth out the garbage collection process for the system. If we
are constantly creating and the disposing of hundreds of particles (for instance), it will take some
time before the disposed objects are actually collected. We could actually be using twice the
memory without the pool if we dispose of 100 objects and then reinstantiate 100 for the next
particle explosion, because the garbage collection process has not necessarily freed up memory
for the disposed objects in time for the next batch to be instantiated.
If we pool the objects, the garbage collector has much less to do, and we will be far less likely to
experience large hiccups in rendering as the garbage collection process starts up and removes
all of the discarded unpooled objects.
Implementing object pooling in our game
We will be using object pools for the player's projectile missiles and the particles for explosions. The
game will set a base amount of objects for each pool. The particle pool will be doubled if the
FrameRateProfiler 's frameRateProfilerAverage can run at 85 percent or more of the desired frame
rate. This is just a very small example of how we can use the render profiling in conjunction with
other optimizations to affect the overall performance of the game on the target player's system.
Note that you can also use the profiled rate as an indication of how to set the stage.quality
attribute to low, medium, high, or best. This affects the quality of the vectors and bitmaps drawn
on the stage. We will use this in the Blaster Mines game.
Creating the technical specifications for object pooling in
Blaster Mines
We will be creating separate manager classes for the pooled objects in our game. These
manager classes will encapsulate the variables needed for object pooling and also the draw
creation functions for the pooled objects.
There will also be a manager class for the Mine enemy craft that the player must shoot, but we
will not be implementing a pool for those objects.
As an example, let's take a look at the pooling code from the ParticleManager class. This class
code will be displayed in its entirely in the section called “Designing the Particle Manager class.”
Adding the private variables
The following private variables will be created in the variable definition section of the
com.efg.games.blastermines.ParticleManager class:
private var particleBitmapData:BitmapData : Holds the look of the particle drawn into
a BitmapData canvas
Search WWH ::




Custom Search