Game Development Reference
In-Depth Information
protected function checkCollisions():void {
var enemyRect:Rectangle;
var projectileRect:Rectangle;
for each (var enemy:Enemy in _enemyList) {
if (!enemy.alive) continue;
enemyRect = enemy.getRect(this);
for each (var projectile:Projectile in _projectileList) {
projectileRect = projectile.getRect(this);
if (enemyRect.intersects(projectileRect)) {
removeProjectile(projectile);
enemy.destroy();
}
}
}
}
'
ll likely notice some similarities between how the projec-
tiles and enemies are each moved. The createEnemy method,
called by the Timer, places new Enemy objects at the right side of
the Stage and they gradually travel across to the opposite side in
the moveEnemies function. Once everything has been moved, the
checkCollisions method runs. It loops through the two lists of pro-
jectiles and enemies and tests rects against each other. If a projec-
tile hits an enemy that is still alive, the enemy will be destroyed.
Note that, at this point, we don
You
t remove the enemy. We rely on
the destroy method of the Enemy class to display the destruction,
and the object will get removed, once it reaches the left side of
the Stage. When you test this SWF,youwillseethatwhenapro-
jectile from the player hits an enemy, it explodes. Add a scoring
mechanism to the number of ships destroyed and a way for the
player to be hurt, and you
'
'
ve got yourself a really simple but com-
plete game!
Weaknesses of This Method
Even though this type of checking is overall pretty thorough, it will
also break down in certain scenarios. If you were to increase the
speed of the ships and the projectiles enough, they would even-
tually reach a point where they would
each other. In a
single frame, they would go from facing each other to passing each
other without a collision being recorded. Granted, they would have
to be traveling very fast
jump over
faster than would probably be practical
for this type of game
t keep the underlying detec-
tion from being fundamentally flawed on some level. Because the
detection is tied to the game
butthatdoesn
'
s frame cycle, it also means that low-
eringtheframeratewilllowerthefrequencyofdetection,effec-
tively creating the same problem I had just mentioned.
'
Search Nedrilad ::




Custom Search