Enemy.as
package
{
import flash.geom.Point;
import mx.core.*;
public class Enemy extends GameObject
{
static public var pool:ResourcePool = new ResourcePool(NewEnemy);
protected var logic:Function = null;
protected var speed:Number = 0;
static public function NewEnemy():Enemy
{
return new Enemy();
}
public function Enemy()
{
super();
}
public function startupBasicEnemy(graphics:GraphicsResource, position:Point, speed:Number):void
{
super.startupGameObject(graphics, position, ZOrders.PlayerZOrder);
logic = basicEnemyLogic;
this.speed = speed;
}
override public function shutdown():void
{
super.shutdown();
logic = null;
}
override public function enterFrame(dt:Number):void
{
if (logic != null)
logic(dt);
}
protected function basicEnemyLogic(dt:Number):void
{
if (position.y > Application.application.height + graphics.bitmap.height )
this.shutdown();
position.y += speed * dt;
}
}
}
This code is very similar to the BackgroundLevelElement code. In fact the only difference is that we have separated the enemy logic into its own function called basicEnemyLogic. We save a reference to the basicEnemyLogic function in the logic property (much like we reference the NewEnemy function in the ResourcePool). This may seem redundant, but will later on allow us to implement new types of enemies by creating new logic functions. The startupBasicEnemy function is used to initialise the underlying GameObject and setup point the logic property to the basicEnemyLogic function, which will n turn be called during the render loop (i.e. the enterFrame function).
With those few simple changes we now have enemies created at specific intervals during the game. The next step is to create some weapons so the player can shoot them. To do this we need to create a Weapon class. Let’s look at the code for that.
Weapon.as
package
{
import flash.geom.*;
public class Weapon extends GameObject
{
static public var pool:ResourcePool = new ResourcePool(NewWeapon);
protected var logic:Function = null;
protected var speed:Number = 0;
static public function NewWeapon():Weapon
{
return new Weapon();
}
public function Weapon()
{
super();
}
public function startupBasicWeapon(graphics:GraphicsResource, position:Point, speed:Number):void
{
super.startupGameObject(graphics, position, ZOrders.PlayerZOrder);
logic = basicWeaponLogic;
this.speed = speed;
}
override public function shutdown():void
{
super.shutdown();
logic = null;
}
override public function enterFrame(dt:Number):void
{
if (logic != null)
logic(dt);
}
protected function basicWeaponLogic(dt:Number):void
{
if (position.y < -graphics.bitmap.height)
this.shutdown();
position.y -= speed * dt;
}
}
}
This code is exactly the same as the Enemy class, except that the basicWeaponLogic function moves the object up the screen instead of down. At this point you might be wondering why we need to separate classes that are almost exact copies of each other. We do this because eventually the enemies and the weapons will have a number of specialised logic routines. While the two could conceivably be integrated into one class, keeping them in separate classes makes the code easier to read. The last change is to the Player class, which will have to watch for a mouse click to start firing the weapons.
Game programming with Flex
Learn how to create a Flash game using Flex with this step by step series.