Wiki

Clone wiki

dsair / Enum

HOWTO: Enum: Using the Enum class to create enums

Introduction

The enums class is based on Scott Bilas' Enum class, which you can find at: http://scottbilas.com/blog/ultimate-as3-fake-enums/

If you don't know what an enum is, check out: http://en.wikipedia.org/wiki/Enumerated_type

Details

Check out the TestEnum.as file for the full details.

As with all examples, you need to init the framework before doing anything:

#!actionscript

DSAir.init( this ); // "this" is your Document class

Creating an Enum

When creating an enum, you simply need to extend the Enum class:

#!actionscript

public class MyEnum extends Enum
{
    { initEnum( MyEnum ) }

    public static const SOME_ENUM_1:MyEnum = new MyEnum;
    public static const SOME_ENUM_2:MyEnum = new MyEnum;
    public static const SOME_ENUM_3:MyEnum = new MyEnum;
}

The

#!actionscript

{ initEnum( MyEnum ) }

line is very important. Placed here, it's what's called a static constructor. Code within the {} brackets is executed the first time the class is referenced. The initEnum() function is the function that performs the magic.

Using an Enum

After your Enum is created, it's easy to use. You can access the name and index by:

#!actionscript

trace( "The enum name is: " + MyEnum.SOME_ENUM_1.name );
trace( "The enum index is: " + MyEnum.SOME_ENUM_1.index);

You can find all the enums for a specific class by calling:

#!actionscript

Enum.getEnum( MyEnum );

Or a specific enum by it's name:

#!actionscript

Enum.getField( MyEnum, "some_enum_1", false ); // false = case-insensitive

or it's index:

#!actionscript

Enum.getByIndex( MyEnum, 2 );

Updated