Game Development Reference
In-Depth Information
}
}
for each (tempBonusPlane in bonusPlaneArray){
if (tempBonusPlane.finished) {
removeItemFromArray(tempBonusPlane,bonusPlaneArray);
} else {
tempBonusPlane.update();
}
}
}
Removing objects
Now, we will create the removeItemFromArray() function to support removing the game objects
from the screen. This is a general-purpose function that takes two values as parameters:
item : This Object is the item to remove from the array
group : This is the Array from which to remove the item.
The function takes the item and uses the Array.indexOf() function to find it. We call the
dispose() method of the object, remove it from the screen with removeChild() , and then
splice() it out of the array represented by group .
This function is by no means perfect. It would be better to use an explicit type instead of the
generic Object type. However, this function saves a lot of time and code, so we have found it very
useful in our projects.
private function removeItemFromArray(item:Object, group:Array) :void {
var index:int = group.indexOf(item);
group[index].dispose();
removeChild(group[index]);
group.splice(index, 1);
}
Detecting basic bitmap collisions
OK, so now we get to the most important function of this entire game, checkCollisions() . This is
the function where nearly everything we have already created, literally, collides head on. This
function is also quite intricate, so we will take it as slowly as possible.
To accomplish the necessary collision detection, we are going to use a built-in Flash function that
is new to ActionScript programmers used to working with MovieClip s only. In previous versions of
ActionScript (and still in AS3), you could easily test for collisions using MovieClip.hitTest() . This
used what is known as bounding box collision detection . With bounding box collision
detection, you test only where the full box around a MovieClip hit another full box around another
MovieClip . It does not matter how intricate the image is inside the box—all that matters were the
boxes. If you draw a box around all the pixels in the image so that every pixel was included, and
that box collided with a similar box around another object, a hit was registered. This actually
works well for games where the objects are rectangular (e.g., Breakout), but for a game like Flak
Cannon with intricately drawn sprites, collision detection with bounding boxes would prove to be
far too inaccurate to be any fun to play at all. See the top of Figure 5-9.
Search WWH ::




Custom Search