Game Development Reference
In-Depth Information
We first load our bullet object ( bullet.json ) in our start function as shown in the
following code:
loadStageObject("model/weapons/bullet.json",[0.0,0.0,0.0],0.0,0.0,0.0
);
We also modified our loadStageObject function to create the object of our Bullet
class. Remember that all the objects are added to the stageObjects array of the
Stage class. As each object inherits StageObject , the drawScene function invokes
the update function of each object independent of its implementation.
function loadStageObject(url,location,rotationX,rotationY,rotationZ){
$.getJSON(url,function(data){
...
else if(nameOfObject==="bullet"){
stageObject=new Bullet();
bullet=stageObject;
}
...
}
}
We have also added a handleKeys function to initialize the bullet each time the
Space bar is hit. The initialize function toggles the visibility of the bullet, and then
the drawScene function starts processing it.
function handleKeys(event) {
switch(event.keyCode) {//determine the key pressed
case 32:// Space Bar
bullet.initialize();
break;
}
}
Reusing objects in multiple bullets
In this section, we would like to cover a very basic concept of reusing objects. In the
preceding code, each time we hit the Space bar, the same bullet changes its location.
What we would actually like is that we generate a new bullet each time the Space bar
is hit. The scene should have multiple bullets. A simple strategy would have been to
clone the bullet each time we hit the Space bar. However, using this way, we would
have multiple objects initialized in the scene and we would have no way to track
them. Hence, we initialize a pool of bullets, and then whenever we hit the Space bar,
we pick a bullet from the pool based on its visibility. If the bullet is not visible, this
means it is not in action and is not being rendered by the drawScene function. We
can safely initialize an invisible bullet completely transparent to the end user.
 
Search WWH ::




Custom Search