Flash Game Development with Flex and Actionscript - Bitmap Animations (Page 4 of 4)

Article by Matthew Casperson (4,883 pts ) , published Nov 6, 2009

Enemy.as

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

Related Files

Showing page 4 of 4

Comments

Oct 22, 2009 6:18 AM
phuong.ho
Using movies for animated objects
why don't you try it by yourself
Jul 28, 2009 4:58 AM
irwin
Using movies for animated objects
This tutorial was great! But it would be really cool if you could use flash movies as animated game objects as well! any suggestions on that?
 
Subscribe to Web Development
RSS
Get free weekly updates, directly to your inbox.
Browse Web Development