Game Development Reference
In-Depth Information
bonusplane: for (ctr2 = bonusPlaneLength; ctr2 >= 0; ctr2--) {
tempBonusPlane = bonusPlaneArray[ctr2];
var bonusPlanePoint:Point = new Point(tempBonusPlane.x, tempBonusPlane.y);
for (ctr = flakLength; ctr >= 0; ctr--) {
tempFlak = flakArray[ctr];
flakPoint = new Point(tempFlak.x, tempFlak.y);
if (tempFlak.image.bitmapData.hitTest(flakPoint,255,
tempBonusPlane.image.bitmapData,bonusPlanePoint)) {
dispatchEvent( new CustomEventSound(CustomEventSound.PLAY_SOUND,
Main.SOUND_BONUS,false,1,0,1));
shots += tempBonusPlane.bonusValue;
dispatchEvent(new CustomEventScoreBoardUpdate(
CustomEventScoreBoardUpdate.UPDATE_TEXT,Main.SCORE_BOARD_SHOTS,
String(shots)));
makeExplosion(tempBonusPlane.x + tempBonusPlane.width/2,
tempBonusPlane.y + tempBonusPlane.height/2);
addToScore(baseBonusPlaneScore+(tempFlak.hits*enemyHitBonus));
tempFlak.hits++;
removeItemFromArray(tempBonusPlane,bonusPlaneArray);
break bonusplane;
}
}
}
By the way, we could have made the bonus planes a special case Enemy object, instead of its
own type. In that case, we would have only had to do the loop once. Finding ways to optimize the
code beyond what is presented in this text is a good way to further hone your game development
skills.
Now, we have to test the player's ships against the
Enemy
planes. Unfortunately, we have to loop
through the
enemyArray
arrays again. Fortunately, we do this for a good reason. Since the
Ship
objects always sit at the same y coordinate on the screen, we can do a quick test to see if we
have to do collision detection based on the current y location of an
Enemy
.
Notice the
ship
: that precedes the
for
loop. This is another label. Again, we will use this label to
break out of a loop if the item we are testing has been removed from the array. Most of the rest of
this should look familiar to you by now.
ship: for (ctr = shipLength; ctr >= 0; ctr--) {
tempShip = shipArray[ctr];
var sPoint:Point = new Point(tempShip.x, tempShip.y);
enemyLength = enemyArray.length-1;
for (ctr2 = enemyLength; ctr2 >= 0; ctr2--) {
tempEnemy = enemyArray[ctr2];
Here is where this loop gets interesting. We will test to see if the current
y
position of the
Enemy
is
close enough to the
Ship
objects for a collision to occur. Since the
Ship
objects are always in the
same place, all we have to do is calculate the first point at which an
Enemy
could possible hit a
Ship
. Otherwise, we skip the test. To do this, we will take the
gameHeight
and subtract the height
of a
Ship
plus subtract
shipYPadding
pixels (because
Ship
s are
shipYPadding
pixels from the
bottom of the screen). Then, we subtract the height of the
Enemy
plane because its registration
point is in the upper-left hand corner. Figure 5-10 shows how we came to this calculation.