Game Development Reference
In-Depth Information
We start the function by creating some variables to hold the lengths of the
Array
objects we will
be testing.
private function checkCollisions():void {
var enemyLength:int = enemyArray.length-1;
var flakLength:int = flakArray.length-1;
var shipLength:int = shipArray.length-1;
var bonusPlaneLength:int = bonusPlaneArray.length-1;
Next, we begin iterating backward through the
enemyArray
array. Recall that
tempEnemy
is a class
variable created one time so that we don't have to waste the processing cycles making a one
every time we go through this loop. Notice the
enemy:
that precedes the
for
loop. This is a
label
.
We will use this label to break out of a loop if the item we are testing has been removed from the
array (this will be explained a bit when we talk about the
break
statement).
enemy: for (var ctr2:int = enemyLength; ctr2 >= 0; ctr2--) {
The first collisions we are going to test are between the
Flak
explosions and the
Enemy
objects.
First, we need to create an instance of the
Point
class. We need a
Point
class for each object
that we are going to test for collisions. We will name this
Point
enemyPoint
. This
Point
represents the upper-left-hand corner of the
Enemy
object.
bitmapData.hitTest()
requires this
location as one of its parameters. Next, we start iterating through
flakArray
, setting
tempFlak
in every iteration.
tempEnemy = enemyArray[ctr2];
var enemyPoint:Point = new Point(tempEnemy.x, tempEnemy.y);
for (var ctr:int = flakLength; ctr >= 0; ctr--) {
tempFlak = flakArray[ctr];
We also need a second
Point
that represents the upper left-hand corner of the
Flak
explosion.
This is required by the
bitmapData.hitTest()
function, so it will know where to start its pixel-level
collision detection.
var flakPoint:Point = new Point(tempFlak.x, tempFlak.y);
Next comes the big boy for this function. This is the
gold
, where everything really happens. This is
the actual
bitmapData.hitTest()
test (inside an
if
statement):
if (tempFlak.image.bitmapData.hitTest(flakPoint,255,
tempEnemy.image.bitmapData,enemyPoint)) {
Let's break down the actual test and parameters of the call to make it abundantly clear what is
actually happening here:
flackPoint
: The upper-left hand corner of the Flak explosion we are testing.
255
: This is the first alpha channel threshold. It needs to be the highest opaque value of
the bitmap, which in our case is 255.
tempEnemy.image.bitmapData
: This is the
bitmapData
of the
Enemy
we are testing
against.
ePoint
: This is the upper-left hand corner of the
Enemy
we are testing.
If a collision is detected, we then move into this section of code. The first thing we do is call the
removeItemFromArray()
function we created earlier passing
tempEnemy
and
enemyArray
array.
Notice that we don't remove the
Flak
explosion. That is because a
Flak
explosion can keep