Using constants

Constants are properties with a fixed value that cannot be altered; in other words, they are values that don't change throughout an application. The ActionScript language contains many predefined constants. For example, the constants BACKSPACE, ENTER, SPACE, and TAB are properties of the Key class and refer to keyboard keys. The constant Key.TAB always has the same meaning: it indicates the Tab key on a keyboard. Constants are useful for comparing values and for using values in your application that do not change.

To test whether the user is pressing the Enter key, you could use the following statement:

var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    if (Key.getCode() == Key.ENTER) {
        trace("Are you ready to play?");
    }
};
Key.addListener(keyListener);

For the previous ActionScript to work, it may be necessary to disable keyboard shortcuts in the authoring environment. Select Control > Test Movie from the main menu, then while previewing the SWF file in the player, select Control > Disable Keyboard Shortcuts from the SWF file's preview window.

In Flash there is no way to create your own constant values except when you create your own custom classes with private member variables. You cannot create a "read-only" variable within Flash.

Variables should be lowercase or mixed-case letters; however, constants (variables that do not change) should be uppercase. Separate words with underscores, as the following ActionScript shows:

var BASE_URL:String = "http://www.adobe.com"; //constant
var MAX_WIDTH:Number = 10;               //constant

Write static constants in uppercase, and separate words with an underscore. Do not directly code numerical constants unless the constant is 1, 0, or -1, which you might use in a for loop as a counter value.

You can use constants for situations in which you need to refer to a property whose value never changes. This helps you find typographical mistakes in your code that you might not find if you use literals. It also lets you change the value in a single place. For more information on literals, see About literals.

For example, the class definition in the next example creates three constants that follow the naming convention used by ActionScript 2.0.

To use constants in an application:

  1. Select File > New and then select ActionScript File to create an AS file.
  2. Name the new file ConstExample.as.
  3. Type the following code into the Script window:
    class ConstExample {
        public static var EXAMPLE_STATIC:String = "Global access";
        public var EXAMPLE_PUBLIC:String = "Public access";
        private var EXAMPLE_PRIVATE:String = "Class access";
    }
    

    The EXAMPLE_STATIC property is a static property, which means that the property applies to the class as a whole instead of to a particular instance of the class. You must access a static property of a class using the name of the class instead of the name of an instance. You cannot access a static property through a class instance.

  4. Create a new Flash document and save it as const.fla.
  5. Open the Actions panel, and type the following code on Frame 1 of the Timeline:
    trace(ConstExample.EXAMPLE_STATIC); // output: Global access
    

    When you declare the EXAMPLE_STATIC property as static, you use this code to access the value of the property.

  6. Select Control > Test Movie to test your document.

    You will see Global access in the Output panel.

  7. In the Actions panel, type this code following the code you added in step 5.
    trace(ConstExample.EXAMPLE_PUBLIC); // error
    trace(ConstExample.EXAMPLE_PRIVATE); // error
    
  8. Select Control > Test Movie to test your document.

    The EXAMPLE_PUBLIC and EXAMPLE_PRIVATE properties are not static properties. When you try to access the values through the class, you see the error message:

    The property being referenced does not have the static attribute.
    

    To access a property that is not static, you must access the value through an instance of the class. Because the EXAMPLE_PUBLIC property is a public property, it is available to code outside of the class definition.

  9. In the Actions panel, delete the trace statements that you added in steps 5 and 7.
  10. Type the following code into the Actions panel:
    var myExample:ConstExample = new ConstExample();
    trace(myExample.EXAMPLE_PUBLIC); // output: Public access
    

    This code instantiates the myExample instance and accesses the EXAMPLE_PUBLIC property.

  11. Select Control > Test Movie to test your document.

    You see Public access in the Output panel.

  12. In the Actions panel, delete the trace statement that you added in step 10.
  13. Type the following code into the Actions panel.
    trace(myExample.EXAMPLE_PRIVATE); // error
    

    The EXAMPLE_PRIVATE property is a private property, so it is available only within the class definition.

  14. Select Control > Test Movie to test your document.

    You see The member is private and cannot be accessed in the Output panel.

    For more information on built-in classes and creating custom classes, see Classes.