How to create an enum in ActionScript 3.

August 5, 2009 at 7:48 pm (ActionScript 3.0) (, , , , )

Hello folks.

Some of us face the issue on how to create an enum in ActionScript 3. It’s no big deal, really!


package
{
    public final class ToolType
    {
        public static const PEN:ToolType = new ToolType();
        public static const ZOOM:ToolType = new ToolType();
        public static const BRUSH:ToolType = new ToolType();
    }
}

As you can see, I’ve created a class named ToolType, I gave it the attribute final as there’s no need to extend the class (unless you wanted some kind of mutated enumerator ;-)).

Each variable is an instance of the class, so that its restricted to the enumeration data type.

That way I can easily do the following:

var toolType:ToolType;
toolType = ToolType.BRUSH;

if (toolType == ToolType.BRUSH)
{
    trace("You have brush selected.");
}

There are many other ways to create enumerations in ActionScript. Here’s a link to Adobe on enumerations:
LINK

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: