AS3 Typesafe Enum - Flash Actionscript Tutorial

1 - I can do better 2 - Jury's out 3 - Pretty darn good 4 - Splendiferous 5 - Awesometastic by 0 people | Log in to rate

Ranked #20,528 in How-To, #214,371 overall

Learn to build a typesafe actionscript 3 enum object.

This flash cs3/as3 tutorial shows you one of the best ways to build an enum object inside the flash environment. Enum objects are best for types and states, and is an extreme help in object state management; or when the state design pattern is a necessity. Being type safe means you can literally cast the object as that type and to be honest, I couldn't live without these beautiful gems!

Must-Read Flash Programming Books 

Here is a collection of AS3, Flash CS3 Programming books that I strongly advise... a little reading never hurt anyone ;)

Why use an enum? 

Enum object description

An enum is an object that allows you to type or set states of other objects. For example, if you have a person object, and need to track the status of that person, with the options of "Active", "WaitingApproved", "Archived", and "Banned", you would like to track, retrieve, and set that state reliably at any time. By attaching an enum to your Person object, each person could track their own state by viewing person.state.

A quick example would be:

[ACTIONSCRIPT 3.0 CODE BLOCK]
var p:Person = new Person("Bob", "Barker", "active");
if(p.state == PersonState.ACTIVE) { Do Something }



This example says we should instantiate a Person object, with first name, Bob, last name Barker, and give them a state of "active", which gets parsed to the PersonState object as "ACTIVE". Now at any time in the life of the application we can say if(p.state == PersonState.STATE_NAME) { Do Something }.

The real power behind this is that we no longer require our memory if our IDE is as excellent as the free tool FlashDevelop. The Intellisense automatically pops up when we say if PersonState. - and the list will pop up for us! Lets talk about how to set this up.

Here's How:

Below we have the source for a DebugType class, with three standard static functions. The getDebugType function converts a string that you pass in, to the type - which makes it beautiful for strongly typing objects directly from XML Values. The toString function converts the type to a string so you can actually see what you are working with. Otherwise, its a bunch of static constants that just point back at itself saying "I'm my own type". Quite a nice little trick... here's the code:

[ACTIONSCRIPT 3.0 CODE BLOCK]

package {
public class DebugType {

public static const NONE:DebugType = new DebugType();
public static const TRACE:DebugType = new DebugType();
public static const TRACE_THROWERROR:DebugType = new DebugType();
public static const TRACE_THROWERROR_LOGREMOTELY:DebugType = new DebugType();
public static const THROWERROR:DebugType = new DebugType();
public static const THROWERROR_LOGREMOTELY:DebugType = new DebugType();
public static const LOGREMOTELY:DebugType = new DebugType();

public static function getDebugType(type:String):DebugType {
var retType:DebugType;
switch(type.toLowerCase()) {
case "logremotely":
retType = DebugType.LOGREMOTELY; break;
case "trace":
retType = DebugType.TRACE; break;
case "trace_throwerror":
retType = DebugType.TRACE_THROWERROR; break;
case "trace_throwerror_logremotely":
retType = DebugType.TRACE_THROWERROR_LOGREMOTELY; break;
case "throwerror":
retType = DebugType.THROWERROR; break;
case "throwerror_logremotely":
retType = DebugType.THROWERROR_LOGREMOTELY; break;
default:
retType = DebugType.NONE; break;
}
return retType;
}

public static function toString(a:DebugType):String {
var retType:String;
switch(a) {
case DebugType.LOGREMOTELY:
retType = "Log Error(s) Remotely Only"; break;
case DebugType.TRACE:
retType = "Trace Error(s) Only"; break;
case DebugType.TRACE_THROWERROR:
retType = "Trace and Throw Error(s)"; break;
case DebugType.TRACE_THROWERROR_LOGREMOTELY:
retType = "Trace, Throw, and Remotely Log Error(s)"; break;
case DebugType.THROWERROR:
retType = "Throw Error(s) Only"; break;
case DebugType.THROWERROR_LOGREMOTELY:
retType = "Throw, and Remotely Log Error(s)"; break;
default:
retType = "No Debug Type Selected"; break;
}
return retType;
}
}
}

The software to get the job done. 

Amazon has the best prices if you would like to buy the Adobe Products (Mainly Flash) but illustrator is one hellova nice app to have as well when working with vectors!

Win Adobe products at a sweet price on eBay 

Loading Fetching new data from eBay now... please stand by
eBay

The latest news regarding Adobe Flash and Actionscript 

Check out the latest news and upcoming sweentess in the world of Adobe. I know Flash 10 is in beta right now so we are at an exciting time to watch for the Flash players new additions and changes...
Graphic Designer
Technical skills to include Photoshop, Illustrator, Flash, ActionScript, Accessibility Awareness and CSS. The successful candidate will have examples of ...
ActionScript for Multiplayer Games and Virtual Worlds
ActionScript for Multiplayer Games and Virtual Worlds is not a comprehensive end-to-end guide to building your own Club Penguin or WebKinz or WoW from the ...
MIPS Technologies Collaborates With Adobe to Optimize Flash Player 10.1 for ...
In October, MIPS Technologies announced the release of a MIPS-optimized version of the ActionScript(TM) virtual machine (VM) that is used in Adobe Flash ...
Kongregate opens the flood gates for MMO developers
Developers can use Javascript, Actionscript, or REST APIs from their own backend to tap into features such as achievements, leaderboards, virtual goods, ...

Reader Feedback 

Give me a shoutout if you have any questions, comments, or just want to tell me a better way to develop an AS3 Enum.

submit

by ccoonen

Flash CS3/AS3 Developer that loves to develop software and websites... (more)

Explore related pages

Create a Lens!