Defining Levels: Game Design With Flex and Actionscript

Written by:  • Edited by: Linda Richter
Updated Nov 6, 2009
• Related Guides: XML

In this guide, 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 example 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.

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 incredible 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

Comments

Showing all 2 comments
 
graphic design quotes Oct 31, 2011 3:37 AM
RE: Defining Levels: Game Design With Flex and Actionscript
Analyze the Price: One of the number one things that you have to account for is how much it will cost to handle the work. Is the price asked by the outsourcer within your budget? If not, then try to bring down the price. There's absolutely nothing wrong in negotiation. Most of the graphic designers will be open for it. Your main purpose is to get the best cost available. This is because if you pay too much, it will increase your pay out costs.<br>
gb Aug 23, 2010 10:07 PM
Sorting? Really?
I've been following pretty well, but your "sort" has me a bit confused. I've been writing software for a long time, and this doesn't look like a sort. But I'm new to AS, which is why I'm reading this, so have mercy.

The LevelDefinitionElement:sort function is actually a compare, behaving much like a c-library strcmp with a return of -1, 0, or 1, but there is no actual object swapping going on here. Over in LevelDefinitions, you appear to call the sort function, but discard the return [levelDefinitions[levelID].sort();], but you are also referencing the sort function (in c terms) by its address [LevelDefinitionElement.sort]. Can you explain in more detail how this is sorting?

Thanks!
 
blog comments powered by Disqus
Email to a friend