package
{
import flash.geom.Point;
import mx.core.*;
public class Enemy extends AnimatedGameObject
{
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.startupAnimatedGameObject(graphics, position, ZOrders.PlayerZOrder);
logic = basicEnemyLogic;
this.speed = speed;
this.collisionName = CollisionIdentifiers.ENEMY;
}
override public function shutdown():void
{
super.shutdown();
logic = null;
}
override public function enterFrame(dt:Number):void
{
super.enterFrame(dt);
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;
}
override public function collision(other:GameObject):void
{
var animatedGameObject:AnimatedGameObject = AnimatedGameObject.pool.ItemFromPool as AnimatedGameObject;
animatedGameObject.startupAnimatedGameObject(
ResourceManager.BigExplosionGraphics,
new Point(
position.x + graphics.bitmap.width / graphics.frames / 2 - ResourceManager.BigExplosionGraphics .bitmap.width / ResourceManager.BigExplosionGraphics.frames / 2,
position.y + graphics.bitmap.height / 2 - ResourceManager.BigExplosionGraphics .bitmap.height / 2),
ZOrders.PlayerZOrder,
true);
this.shutdown();
}
}
}
By extending AnimatedGameObject instead of GameObject we allow the Enemy class to be animated. This also requires calling super.startupAnimatedGameObject instead of super.startupGameObject in our startup function.
The only other change is in the collision function, where we create an AnimatedGameObject to represent the explosion. Notice that we set playOnce to true in the constructor. This means that the explosion will be created, then play through once before removing itself from the game.
Animations add a whole new level of detail to a game. By creating the AnimatedGameObject class we now have the ability to quickly and easily add animated object to the game. The only thing missing now are some sound effects, which we will add in part 8.
Go back to Flash Game Development with Flex and ActionScript