Game Development Reference
In-Depth Information
Concepts like these actually make your game successful. This way, even if the game
is being rendered for a long period of time without a pause, the game performance
will not deteriorate.
Let's look at the implementation of the concept closely. Open
06-Multiple-Bullets-
Action.html
in your editor.
We start with the definition of our variables, a
bullets[]
array that will hold the pool
of bullets, and
nextBulletIndex
that will hold the index of the next bullet to be used.
var bullets=[];
var nextBulletIndex=0;
We just added the
loadStageObjects
and
addStageObjects
functions here.
We discussed them earlier; we added them to make the flow more intuitive.
After each object is loaded in
loadStageObject
, it invokes
addStageObject
which
in turn invokes
cloneObjects
. The
cloneObjects
function checks for the object
type and then creates its corresponding clones by invoking the
clone
function of the
object. For the bullet, it creates 24 clones (one was already loaded, in total 25) and
then adds them to the
bullets
array. Each bullet also holds the reference to the
cam
object in its
camera
property.
function loadStageObject(url,location,rotationX,rotationY,rotationZ){
...
addStageObject(stageObject,location,rotationX,rotationY,rotationZ);
...
}
function addStageObject(stageObject,location,rotationX,rotationY,rota
tionZ){
cloneObjects(stageObject);
stageObject.location=location;
stageObject.rotationX=rotationX;
stageObject.rotationY=rotationY;
stageObject.rotationZ=rotationZ;
stage.addModel(stageObject);
}
function cloneObjects(stageObject){
...
if(stageObject.name=="bullet"){
bullets.push(stageObject);
for(var j=0;j<24;++j){
var bullet=stageObject.clone();
bullet.camera=cam;