Being killed by an enemy

Both enemies and the spaceship have a perfect circular shape. This will help us to determine when an enemy and the spaceship collide. Being basically two circles, we can say they collide when the distance between their centers is less than the sum of both the radius.

Let's start creating a quick function to determine the distance between two Sprites using the Pythagorean Theorem:

private function distance(from:Sprite,to:Sprite):Number {
var distX:Number=from.x-to.x;
var distY:Number=from.y-to.y;
return distX*distX+distY*distY;

There isn't that much to explain, since we are just applying a world famous formula, but I want you to notice I am not performing any square root because it's quite CPU-expensive. It won't be a ...

Get Flash Game Development by Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.