Friday, January 11, 2013

Constants

ActionScript 3.0 supports the const statement, which you can use to create constants. Constants are properties with a fixed value that cannot be altered. You can assign a value to a constant only once, and the assignment must occur in close proximity to the declaration of the constant. For example, if a constant is declared as a member of a class, you can assign a value to that constant only as part of the declaration or inside the class constructor. The following code declares two constants. The first constant, MINIMUM, has a value assigned as part of the declaration statement. The second constant, MAXIMUM, has a value assigned in the constructor.
class A {
     public const MINIMUM:int = 0;
     public const MAXIMUM:int;
     public function A() {
          MAXIMUM = 10;
     }
}
var a:A = new A();
trace(a.MINIMUM); // Output: 0
trace(a.MAXIMUM); // Output: 10
An error results if you attempt to assign an initial value to a constant in any other way. Forexample, if you attempt to set the initial value of MAXIMUM outside the class, a runtime error
will occur.
class A {
     public const MINIMUM:int = 0;
     public const MAXIMUM:int;
}
var a:A = new A();
a["MAXIMUM"] = 10 // runtime error
The Flash Player API defines a wide range of constants for your use. By convention, constants in ActionScript use all capital letters, with words separated by the underscore character ( _ ). For example, the MouseEvent class definition uses this naming convention for its constants, each of which represents an event related to mouse input.
package flash.events {
     public class MouseEvent extends Event {
          public static const CLICK:String = "click";
          public static const DOUBLE_CLICK:String = "doubleClick";
          public static const MOUSE_DOWN:String = "mouseDown";
          public static const MOUSE_MOVE:String = "mouseMove";
       }
}
Programming Actionscript 3. Powered by Blogger.