• Home
  • Contact
  • Careers
  • Socialize With Us:
  • Send us Mail
  • Follow us on Twitter
  • Join our Facebook Group
  • /
  • Linkedin
  • /
  • Vimeo Videos
  • /
  • Youtube
  • Subscribe to our RSS Feed
  • Search Site

  • About UsAwards & Bio
  • PortfolioOur Latest Work
  • TrainingTips & Tutorials
  • BlogGreat Articles

You are here: Home / Training / Quick Tips / Using Enums in AS3

Using Enums in AS3

Category: Quick Tips     |     Tags: AS3, AS3.5

Enums are my favorite community developed language feature!!!

UPDATED: I have incorporated all comments into a new version of my demo, and updated the post and source code! Old comments are cleared.

An Enum is a kind of variable that is not natively available in ActionScript 3.0, but can be found in other languages such as C++. Perhaps Enums will be included in the upcoming ActionScript 4.0 when it is released.

Basically – An Enum is a type with a restricted set of values. Typically developers use a string to hold simple state variables such as this;

//USING STRINGS (NOT ENUMS)
public static var STATE_ENABLED : String = "STATE_ENABLED";
public static var STATE_DISABLED : String = "STATE_DISABLED";
private var _currentState_str : String = STATE_ENABLED;

Is this familiar? I’m sure it is. By using the static variable the benefit is that developers are less likely to misspell the name of one of these values due to helpful authortime and compile time errors. However the type is stored as a simple string so the developer *CAN* easily pass the wrong string in there (because ANY string is ‘correct’ to the compiler’). This is where Enums are great. Enums would work like this;

//USING ENUMS

// 1. STRONG TYPED & NICE USAGE
var current_estate : StateEnum = StateEnum.Connecting;

// traces 'Connecting'. Nice!
trace ("Instance  : " + current_estate);

// traces 'true'
trace ("TypeCheck : " + (current_estate is StateEnum) );

// trace 'true' - This is the most common usage.
// Checking if a StateEnum instance matches a kind of StateEnum
trace ("Match?    : " + (current_estate == StateEnum.Connecting) );

// 2. PROPERLY THROWS AN ERROR - YOU CAN'T INSTANTIATE StateEnum Outside of StateEnum.as
//var local_enum : StateEnum = new StateEnum ();

Next Steps

  • Don’t understand me? I don’t blame you. It’s crazy. Check out the source (See ‘Member Resources’ below).

Member Resources

'Free Member'-Only Content

You must be a 'Free Member' to see this content. With your access you will enjoy members-only content like this, HD video tutorials, and access to complete source code.

Ready To Join?
  • Sign up for 'Free Member' level now. Its free!
  • Sign up for 'Paid Member' level to enjoy much, much more! (Invite Only)
Already A Free Member?
  • Log in and enjoy!

6 Responses to Using Enums in AS3

  1. SydField2.0 says:
    February 17, 2010 at 12:00 pm

    I tried your enum class approach once. I found several problems with this: Flash requires constructors to be public, and since no actual polymorphism is present, you can’t get away with creating a default constructor and then a private one. The programmer can still instance another EnabledEnum and use it.

    Reply
    • srivello says:
      March 12, 2012 at 8:21 pm

      Updated.

      Now the programmer cannot ‘instance another’ Enum.

      Reply
  2. nicely done says:
    October 5, 2010 at 1:50 pm

    this is the closest for as3 i have seen, been worthy of a authors note in the code ;)

    Reply
  3. Thijs says:
    February 24, 2012 at 9:06 am

    In your approach it’s still possible to create a new instance of the enum class and use this value:

    current_estate = new StateEnum();

    This would brake you code.

    Have you seen the Enumerator class of the Temple Library: http://templelibrary.googlecode.com/svn/trunk/modules/common/doc/temple/common/enum/Enumerator.html

    With this class it’s not possible to create a new instance of the Enumerator outside of the (extended) Enumerator class.

    And it’s even possible to serialize / deserialize the Enumerator using JSON, as you can see in the following example:
    http://templelibrary.googlecode.com/svn/trunk/examples/index.html#JSON

    Reply
    • srivello says:
      March 12, 2012 at 8:22 pm

      Updated.

      Now the programmer cannot ‘instance another’ Enum. The unique benefit of your link is the JSON serialization. That is indeed cool and my demo does not do that.

      Reply
  4. Alan Shaw says:
    March 13, 2012 at 5:27 pm

    Please have a look at https://github.com/nodename/Ultimate-Enum

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Free Member Login

You are not currently logged in.






» Register
» Lost your Password?

Support Our Sponsors

Category

  • Industry News
  • Standards & Best Practices
  • Full Tutorials
  • RMC News
  • Events

Tag

3D AIR API AS3 AS3.5 Business Experimental Flash Flex Games HTML5 Mobile PushButtonEngine Robotlegs Smash WordPress WordPress Plugin

Brazilean Developers

  • Abendita.com
  • dclick.com.br
  • dm9.com.br
  • Fellyph Cintra
  • IgorCosta.org
  • MonadaSolucoes.com.br
  • PossibleWorldwide.com.br
  • Unit9.com

Developers

  • Adobe Blogs
  • Ben Forta
  • Colin Moock
  • Enrique Duvos
  • Flash Mobile Blog
  • Jess Freeman
  • Kevin Hoyt
  • Lee Brimelow
  • Paul Trani
  • Quasimondo
  • Renaun Erickson
  • Ryan Stewart

Free Assets

  • Free Sounds
  • HasGrafics

HTML5 Games

  • Closure JS Library
  • Eloquent JS Manual
  • Game Framework – CraftyJS
  • Game Framework – EaselJS

Italian Developers

  • alchimedia.com
  • corlan.org/
  • creativesource.it
  • dimix.it
  • fabiobiondi.com
  • gnstudio.com
  • Interpreting-tech.com/bemobile/
  • leonardorisuleo.info
  • lucamascaro.info
  • mart3.org
  • mxml.it
  • nxn.it
  • pirosoft.it
  • Preload.it
  • sonnati.wordpress.com/
  • webgriffe.com

Products

  • Adobe.com
  • Amazon Kindle E-Reader
  • ElectroServer
  • F*CSS
  • Flash Development Toolkit (FDT)
  • O'Reilly PureMVC Book
  • Samsung Galaxy Tablet
  • Unity3D

RMC

  • RMC Consulting

Spanish Developers

  • Flash Adictos
  • HTML Cinqo
  • Tutoriales Flash

Tutorial

  • Active Tuts
  • AS3-to-Unity3D Training Videos
  • Doing 2D in Unity3D
  • Learning C#
  • Unity3D Tutorials

Unity3D Games

  • AS3-to-Unity3D Training Videos
  • Doing 2D in Unity3D
  • Learning C#
  • Matt Eley's Blog
  • Unity3D
  • Unity3D Tools
  • Unity3D Tutorials

I Am Great!

   

Latest Portfolio

  • Happy Birthday Mobile AppMarch 14, 2012, 5:55 pm
  • Official Robotlegs MVCS DiagramFebruary 24, 2012, 1:36 am
  • Health Education Application For WebJanuary 28, 2012, 2:46 am
  • Adobe DevNet’s Flash Gaming Feature ArticleJanuary 27, 2012, 11:49 pm

Latest News

  • Considering Flash-To-Mobile DevelopmentMay 1, 2012, 8:26 am
  • Node.js for Flash & Flex IntroductionApril 1, 2012, 7:50 pm
  • Will HTML5 Ever Catch Up to Flash on Features?March 30, 2012, 5:28 pm
  • Alternativa3D Engine for Adobe Flash Goes Open SourceMarch 29, 2012, 1:56 pm

Latest Tweets (@srivello)

  • How AppsFire Rates Mobile Apps - http://t.co/gklNjn6t
  • MacOSX "Stationery Pad" - Great feature - http://t.co/FZEJibYZ
  • Harvard - 6 Myths of Software Development - http://t.co/D3OTenrm
  • AS3 Developer Considers HTML5 - Overview - http://t.co/3xOyVLVD

© Copyright 2006 - 2012 - Rivello Multimedia Consulting - Flash / Flex / HTML5 Game And App Development With Tutorials