Game Development Reference
In-Depth Information
for (b in this.heroBullets) {
bullet = this.heroBullets[b];
collision = ndgmr.checkPixelCollision(enemy, bullet);
if (collision) {
enemy.takeDamage();
bullet.shouldDie = true;
}
}
}
}
p.checkEnemyBullets = function () {
var b, bullet, collision;
for (b in this.enemyBullets) {
bullet = this.enemyBullets[b];
collision = ndgmr.checkPixelCollision(this.heroShip, bullet);
if (collision) {
bullet.shouldDie = true;
this.heroShip.takeDamage();
this.healthMeter.takeDamage(10);
}
}
}
p.checkShips = function () {
var enemy, i;
var len = this.enemies.length - 1;
for (i = len; i >= 0; i--) {
enemy = this.enemies[i];
if (enemy.y > screen_height / 2) {
collision = ndgmr.checkPixelCollision(this.heroShip, enemy);
if (collision) {
this.removeChild(enemy);
this.enemies.splice(i, 1);
this.spawnEnemyExplosion(enemy.x, enemy.y);
this.heroShip.shouldDie = true;
break;
}
}
}
}
All hero bullets are checked to see if they collide with any enemy ship. If a bullet collision is detected on an
enemy ship, its takeDamage method is invoked, and the bullet's shouldDie is set to true so it can be disposed of in the
next cycle. The same approach is used to check enemy bullets against the hero. A collision there will cause damage to
the ship and update the health meter as well.
When detecting ship-on-ship collisions, the check on each enemy's position is first run to determine if the enemy
is anywhere near the ship. This is simply done by using the middle of the screen. If the enemy is not passed this point,
then you know the check would be wasteful. With a ship-on-ship collision, the enemy should explode instantly. In
other words, you shouldn't wait till the next tick to determine if the enemy should die, and the destruction process
should happen now. This is done because the hero's death will temporarily stop the update/render cycle. The hero's
fate is determined by its own upcoming check function, so its shouldDie property is simply set to true.
Search WWH ::




Custom Search