Moving From Flash AS2 To Flash AS3: Static vs. Const

(Note: I’m converting my Flash game “Spin City” from AS2 to AS3 so it can be reskinned for another project. I’ve decided to document this activity in a series of tutorials so they might benefit anyone else who is going through the same development process)

Constant Variables

In Flash AS1 and AS2 there was no support for constants. A constant (usually designed with const) is a variable that represents a value that never changes. At first, this sounds fairly useless. Why would you want a variable that never changes? Isn’t a variable, by it’s very definition supposed to change?

However, constants come in very handy, mostly for code maintenance. A very simple example would be constant used in game for friction. Let’s say that all on-screen objects will degrade their speed .02 pixels per frame because of friction on the ground. Since there may be be dozens of places that you might apply this value for friction, you would have to hard-code .02 . This is great, unless you want to change the value and have it apply globally in your class. In that case, it is easier to create a const and use it in your code. The problem with AS2 was that there was no support for constants.

Static Variables

Instead of using constants in and AS2, I would default to using static variables, a concept that is supported in AS2. This was only for my own reference, as the static keyword defines a variable slightly different (but no less important) than const. A static is also known as a class variable. These are values set in a class that can be retrieved by a static reference to a class. A static reference is when you use an uninstantiated class identifier to access a function or attribute of a class that has been itself designated as static. Another feature of static variables is that there is only one value for all instances of a class, so if you change it in one instance, it changes it for all. However, it is rare that you will actually do this. In fact, many static variables are really just consts and should be treated as such, but AS2 would not allow it.

For my SpinCity ->FashionDash AS2->AS3 re-skin/conversion, I have many static variables used for the purpose static and const .

Flash AS2

In the Game class, there are a set of variable set as static that are used for tuning the game. Some examples would be:

[cc lang=”javascript” width=”550″]
    static var MAXLEVELSECONDS:Number = 150;
static var CUSTOMER_ARRIVAL_INTERVAL:Number = 10;
static var CUSTOMER_ARRIVAL_LOW_LIMIT:Number = 6;
static var CUSTOMER_MAX_NEEDS_BASE:Number = 2;

These values are accessed from other classes through a static reference to the Game class (i.e Game.MAXLEVELSECONS ) . These are the types of static variables that should remain static in AS3;

Also in the Game class I have a whole set of static variables that are used my simplified state machine. These are states Game can take at any one time.

[cc lang=”javascript” width=”550″]
static var STATE_INIT:Number  =5;
static var STATE_SHOW_TITLE_SCREEN:Number  =10;
static var STATE_TITLE_SCREEN_WAIT:Number  =20;
static var STATE_SHOW_DIFFICULTY_SCREEN:Number          =30;
static var STATE_DIFFICULTY_SCREEN_WAIT:Number          =40;
static var STATE_SHOW_INSTRUCTIONS_SCREEN:Number         =50;
static var STATE_LEVEL_SCREEN_WAIT:Number               =100;
static var STATE_START_LEVEL:Number =110;
static var STATE_GAME:Number =120;


In reality, these should not be static variables. They never change, should never change, and are never accessed outside of this class. These should be declared as const, but that is not possible in AS2

Flash AS3

In Flash AS3 I can finally declare these variables in the proper, Object Oriented, encapsulated manner. First off, are the game tuning variables. There are three basic attributes that we can define about them:

  1. They never change (const)
  2. They need to be accessed from other classes (public)
  3. They need to be referenced statically (without an instance of the class) (static)

We can now properly define these variables like this:

[cc lang=”javascript” width=”550″]
    public static const MAXLEVELSECONDS:Number = 150;
public static const CUSTOMER_ARRIVAL_INTERVAL:Number       = 10;
public static const CUSTOMER_ARRIVAL_LEVEL_MODIFIER:Number       = .5;
public static const CUSTOMER_ARRIVAL_LOW_LIMIT:Number       = 6;
public static const CUSTOMER_MAX_NEEDS_BASE:Number       = 2;
public static const CUSTOMER_MAX_NEEDS_LEVEL_INCREMENT_TRIGGER:Number       = 10;
public static const CUSTOMER_MULTIPLE_NEEDS_CHANCE_BASE:Number       = 50;
public static const CUSTOMER_MULTIPLE_NEEDS_CHANCE_LEVEL_MODIFIER:Number      = 10;

The state machine variables can be similarly defined with the proper access rights and properties. There are two basic attributes we can define about these variables:

  1. They will never change
  2. They will only be used by the Game class (private)

We can now properly define these variables like this:

[cc lang=”javascript” width=”550″]
private const STATE_INIT:Number = 5;
private const STATE_SHOW_TITLE_SCREEN:Number = 10;
private const STATE_TITLE_SCREEN_WAIT:Number = 20;
private const STATE_SHOW_DIFFICULTY_SCREEN:Number = 30;
private const STATE_DIFFICULTY_SCREEN_WAIT:Number = 40;
private const STATE_SHOW_INSTRUCTIONS_SCREEN:Number = 50;
private const STATE_LEVEL_SCREEN_WAIT:Number = 100;
private const STATE_START_LEVEL:Number = 110;
private const STATE_GAME:Number = 120;


So, there you have it. Another very basic lesson about transitioning code from Flash AS2 to AS3. nothing earth shattering, still very useful.


Leave a Reply