Flash Game Development with Flex and Actionscript - Defining a Level

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

In this article we implement a system to define a level within the game.

To this point our "level" has really just been one endless stream of randomly placed enemies. Obviously this is not ideal as it gives us as the developer no control over how the level plays out. In order for us to provide a structured level design we need a way to define and save that level structure. It sounds like a simple challenge, but we have many options each with their own advantages and disadvantages.

My first instinct was to define the level in XML. Actionscript has excellent support for XML, allowing you to declare an XML variable directly in the code. Similarly Actionscript also provides an easy interface for traversing an XML document. The downside to using XML is that you need to supply the code to interpret the XML nodes. For exampe you may have an XML node that defines an enemy placement. To turn this node into an actual object the XML attributes or child nodes need to be parsed, stored and then passed into the function used to actually create the enemy object. While not a difficult task, it is tedious to write.

Thankfully Actionscript gives us another possibility. By using a Function object Actionscript can treat any function as an object, which can then be passed around and stored like any other object. What's more we can assign anonymous functions to the Function constructor. We can use this to create an anonymous function that directly creates a new enemy object, which is then stored in a Function object to be called at a certain point during the level. This sounds complicated, but will become clearer with some example code.

The first class we need to create to store our level definitions is the LevelDefinitionElement. Lets look at that code now.

LevelDefinitionElement.as

package

{

public class LevelDefinitionElement

{

public var time:Number = 0;

public var func:Function = null;

public function LevelDefinitionElement(time:Number, func:Function)

{

this.time = time;

this.func = func;

}

static public function sort(objectA:LevelDefinitionElement, objectB:LevelDefinitionElement):int

{

if (objectA.time < objectB.time) return -1;

if (objectA.time == objectB.time) return 0;

return 1;

}

}

}

The purpose of this class is to save a Function object that will be called after a certain amount of time has passed in the level. For example you might want to create an enemy fighter that appears 10 seconds into the game.

It has two properties: time and func. The time property defines the point during the level when the func Function will be called. The func property contains a function to be executed. This function can do anything from creating a new enemy, creating a new background element, playing a sound effect etc. In fact because we can assign any function to this property we have increadible flexibility over how we define the level structure.

The sort function is used to sort the LevelDefinitionElements in an array, with those with a smaller time appearing before those with a larger time.

The LevelDefinitions class will serve as a container for the many LevelDefinitionElements that we will eventually need to define a cmplete level structure. Lets look at that code now.

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