Game Development Reference
In-Depth Information
These are the collisions we look for in this function:
When any missile in the
playerMissiles
array hits an enemy tank
: Enemy tanks
have
healthPoints
just like the player. When all
healthPoints
are gone, the enemy tank
is destroyed.
Missiles in the
enemyMissiles
array hitting the
player
: Each time the player is hit by
an enemy missile, one of its
healthPoints
is removed. When all five are gone, the
player tank explodes. If the player is out of lives,
gameOver
is set to
true
.
The
player
hitting the
goalSprite
: As soon as the player hits the
goalSprite
, the level
is over.
The
player
hitting any
BlitSprite
in the
ammoPickupList
array
: The
ammo
variable is
increased by the
ammoPickupAmount
value when this is true.
The
player
hitting any
BlitSprite
in the
lifePickups
array
: The lives variable is
increased by 1 when this is true.
Here's the new function's code:
private function checkCollisions():void {
//loop through playerMissiles and check against enemy
var playerMissileLength:int = playerMissileList.length - 1;
playerHitPoint.x = player.nextX;
playerHitPoint.y = player.nextY;
var enemyLength:int = enemyList.length - 1;
missiles: for (var ctr:int = playerMissileLength; ctr >= 0; ctr--) {
tempMissile = playerMissileList[ctr]
for (var ctr2:int = enemyLength; ctr2 >= 0; ctr2--) {
tempEnemy = enemyList[ctr2];
missileHitPoint.x = tempMissile.nextX;
missileHitPoint.y = tempMissile.nextY;
enemyHitPoint.x = tempEnemy.nextX;
enemyHitPoint.y = tempEnemy.nextY;
if (tempMissile.bitmapData.hitTest(missileHitPoint,
255, tempEnemy.bitmapData, enemyHitPoint)) {
//trace("hit enemy");
tempEnemy.healthPoints--;
if (tempEnemy.healthPoints < 1) {
createExplode(EXPLODE_LARGE, tempEnemy.x, tempEnemy.y);
dispatchEvent(new CustomEventSound(CustomEventSound.PLAY_SOUND,
Main.SOUND_EXPLODE, false, 1, 0));
enemyList.splice(ctr2, 1);
dispose(tempEnemy);
score += scoreEnemy;
}else {
createExplode(EXPLODE_SMALL, tempMissile.x, tempMissile.y);