Flash Game Development with Flex and Actionscript - Embedding Resources and Adding Game Objects (Page 2 of 4)

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

GameObject.as

package

{

import flash.display.*;

import flash.events.*;

import flash.geom.*;

/*

The base class for all objects in the game.

*/

public class GameObject

{

// object position

public var position:Point = new Point(0, 0);

// higher zOrder objects are rendered on top of lower ones

public var zOrder:int = 0;

// the bitmap data to display

public var graphics:GraphicsResource = null;

// true if the object is active in the game

public var inuse:Boolean = false;

public function GameObject()

{

}

public function startupGameObject(graphics:GraphicsResource, position:Point, z:int = 0):void

{

if (!inuse)

{

this.graphics = graphics;

this.zOrder = z;

this.position = position.clone();

this.inuse = true;

GameObjectManager.Instance.addGameObject(this);

}

}

public function shutdown():void

{

if (inuse)

{

graphics = null;

inuse = false;

GameObjectManager.Instance.removeGameObject(this);

}

}

public function copyToBackBuffer(db:BitmapData):void

{

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

}

public function enterFrame(dt:Number):void

{

}

}

}

This class is designed to be extended to create more specific game elements like bullets, the player, the enemies etc. It has 4 properties. The position property simply defines the position of the GameObject on the screen. The coordinates (0,0) indicate a position in the top left of the screen, and the coordinates (1,1) indicate a position on the bottom right. The zOrder property defines the “height” of the object on the screen. A GameObject with a low zOrder (like a building on the ground) will be drawn beneath a GameObject with a higher zOrder (like the player). The graphics property is a reference to a graphics resource, which will be described later. This graphics resource is the picture that gets drawn to the back buffer. And finally the inuse property simply flags the object as being active in the game. We will use this property to allow the use of resource pooling later on.

The startupGameObject and shutdown functions should start to look familiar. The startup and shutdown pair of functions will be a common theme as we add more classes. In the GameObject they simply initialise variables, and add or remove the GameObject from the collection maintained by the GameObjectManager. The copyToBackBuffer function takes the pictures stored in the graphics property and copies them to the back buffer. The enterFrame function here is empty. This is because the GameObject is designed to be extended by more specialised classes, and it’s these specialised classes that will add logic inside the enterFrame function. The Bounce class is an example of how this is done.

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