Part 3: Enemy Missiles
We now need to create a class for the EnemyMissile that will be shot by the Enemy at the player. Luckily, we have already done most of this work in Lesson 1. Essentially, EnemyMissile is the same as Missile, but it travels down the screen.
There are two main differences between the old Missile.as class, and the new EnemyMissile.as class. The first is that we have added the same disappear functionality as we added to Enemy. This was added so that the EnemyMissiles (as well as Enemies and Missiles) could be gracefully removed from the playfield when the player’s ship is destroyed. (Note, we have added this functionality to the Missile class as well, and while it won’t be discussed here, you can see the code in Missile.as ).
The second difference is that we simply move the Missile down the screen in Move() ( nexty = _y + speed;) and check to see that it is off the top of the screen before we romove it (if (_y > finishedY) ). The full code for EnemyMissile is below:
import mx.events.EventDispatcher;
class EnemyMissile extends MovieClip {
var speed:Number =0;
var nexty:Number;
var nextx:Number;
var offscreen:Boolean = false;
var finishedY:Number = 0;
var disappear:Boolean = false;
function EnemyMissile() {
EventDispatcher.initialize(this);
offscreen = false;
}
function setDisappear(b:Boolean) {
disappear = b;
}
function setSpeed(s:Number) {
speed = s;
}
public function setLocation(xp:Number, yp:Number) {
_x = xp;
_y = yp;
}
function setFinishedY(yp) {
//trace("set finished Y e missile:" + yp);
finishedY = yp;
}
function Move() {
//trace("Move Enemy Missile:" + this + "," + _x + "," + _y);
nexty = _y + speed;
nextx = _x;
}
function Render() {
if (!disappear) {
_y = nexty;
_x = nextx;
if (_y > finishedY) {
this.dispatchEvent({type:"EventEnemyMissileOffScreen", missile:this});
}
} else {
this._alpha -= 10;
if (this._alpha <= 0) {
this.dispatchEvent({type:"EventEnemyMissileOffScreen", missile:this});
}
}
}
public function addEventListener(){/*Interface Stub*/}
public function removeEventListener(){/*Interface Stub*/}
public function dispatchEvent(){/*Interface Stub*/}
}
Read the rest of the series: ‘Anatomy of a Flash Game’
- Anatomy of a Flash Game: Lesson 1 – Setting up the game
- Anatomy of a Flash Game: Lesson 2 – Creating Enemies & The Game Environment
- Anatomy of a Flash Game: Lesson 3 – MochiAds, MochiBot and MochiAds Leaderboards

very good