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

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

AnimatedGameObject.as

package

{

import flash.display.*;

import flash.geom.*;

import mx.collections.*;

public class AnimatedGameObject extends GameObject

{

static public var pool:ResourcePool = new ResourcePool(NewAnimatedGameObject);

protected var frameTime:Number = 0;

protected var currentFrame:int = 0;

protected var frameWidth:int = 0;

protected var playOnce:Boolean = false;

static public function NewAnimatedGameObject():AnimatedGameObject

{

return new AnimatedGameObject();

}

public function AnimatedGameObject()

{

}

public function startupAnimatedGameObject(graphics:GraphicsResource, position:Point, z:int = 0, playOnce:Boolean = false):void

{

this.playOnce = playOnce;

this.frameWidth = graphics.bitmap.width / graphics.frames;

startupGameObject(graphics, position, z);

}

override public function enterFrame(dt:Number):void

{

if (inuse)

{

frameTime += dt;

if (graphics.fps != -1)

{

while (frameTime > 1/graphics.fps)

{

frameTime -= 1/graphics.fps;

currentFrame = (currentFrame + 1) % graphics.frames;

if (currentFrame == 0 && playOnce)

{

shutdown();

break;

}

}

}

}

}

override public function copyToBackBuffer(db:BitmapData):void

{

if (inuse)

{

var drawRect:Rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, graphics.bitmap.height);

db.copyPixels(graphics.bitmap, drawRect, position, graphics.bitmapAlpha, new Point(drawRect.x, 0), true);

}

}

override protected function setupCollision():void

{

collisionArea = new Rectangle(0, 0, frameWidth, graphics.bitmap.height);

}

}

}

Like any other game resource, the AnimatedGameObject class extends GameObject. To play an animation we override 3 of GameObjects functions: enterFrame, copyToBackBuffer and setupCollision.

During enterFrame an AnimatedGameObject keeps a track of how long it has been since the last frame was played. Once this reaches the value defined by the fps property of the GraphicsResource the AnimatedGameObject moves to the next frame. There is also a playOnce property, which if set to true triggers the AnimatedGameObject to remove itself from the game once one complete animation has been played. This will be useful for creating effects (like an explosion) that play once and should then dissappear. It allows you as a coder to create a kind of "fire and forget" object in the game that will clean itself up.

The copyToBackBuffer has been modified slightly from the same code in the GameObject to allow it to only copy a section of the image referenced by the grahipcs property. This section relates to the current frame, and as the section that is moves from left to right we appear to get an animation on the screen.

Finally we have overridden the setupCollision function to define a collisionArea that is the size of one animated frame as opposed to the size of the whole image.

The rest of the code here (like the resource pooling and startup/shutdown functions) have been discussed in previous articles, and their application here follows the same mentality.

We also need to make some changes to the ResourceManager to accomodate the extra information required by an animation. Lets take a look at that now.

Showing page 2 of 4
Subscribe to Web Development
RSS
Get free weekly updates, directly to your inbox.
Browse Web Development