Digital Media WebWeb > Features

ActionScript 3.0 Cookbook: Chapter 2, Custom Classes

ActionScript 3.0 Cookbook: Chapter 2, Custom Classes
Pages: 1, 2, 3, 4

Section 2.7: Creating Constants

Problem

You want to declare a constant.

Solution

Declare it just like you would declare a property, except use the const keyword in place of var.

Discussion

As the name constant implies, constant values do not change. Constants are useful when you have complex values that you want to be able to reference by a simple identifier or when you want to be able to use compile-time error checking for values. Math.PI is an example of a constant that contains a complex value (which is the value of pi, or 3.14159). MouseEvent.MOUSE_UP, which contains the value mouseUp, is an example of a constant that allows you to use error-checking. When you add an event listener for the mouse up event, you can use the string value mouseUp. However, if you accidentally have a typo, you won't be notified of an error, and your code won't work as expected:

// This is valid code, but because of the typo (mousUp instead of mouseUp) the 
// code won't work as expected.
addEventListener("mousUp", onMouseUp);

Using a constant helps. If you accidentally misspell the constant, you will receive a compile error that helps you track down the error:

// This causes a compile error.
addEventListener(MouseEvent.MOUS_UP, onMouseUp);

The syntax for declaring a constant is very similar to that for declaring a standard property. However, rather than using the var keyword you use the const keyword. Although not required, the majority of constants also happen to be public and static. If you want a constant to be public and static, you must use the correct attributes. Additionally, you must assign a value for a constant when declaring it:

static public const EXAMPLE:String = "example";

By convention, constant names are all in uppercase. This convention makes it easy to identify and differentiate constants from properties.

See Also

Recipe 2.4

Section 2.8: Dispatching Events

Problem

You want to dispatch events.

Solution

Extend flash.events.EventDispatcher and call the dispatchEvent( ) method.

Discussion


This excerpt is from ActionScript 3.0 Cookbook. Well before Ajax and Windows Presentation Foundation, Macromedia Flash provided the first method for building "rich" web pages. Now, Adobe is making Flash a full-fledged development environment, and learning ActionScript 3.0 is key. That's a challenge for even the most experienced Flash developer. This Cookbook offers more than 300 solutions to solve a wide range of coding dilemmas, so you can learn to work with the new version right away.

buy button

Events are an important way for objects to communicate. They are essential for creating flexible systems. Flash Player 9, for example, has a built-in event dispatching mechanism in the flash.events.EventDispatcher class. All classes that dispatch events inherit from EventDispatcher (e.g., NetStream and Sprite). If you want to define a class that dispatches events, you can extend EventDispatcher, as follows:

package {
    import flash.events.EventDispatcher;
    public class Example extends EventDispatcher {

    }
}

The EventDispatcher class has public methods called addEventListener( ) and removeEventListener( ) that you can call from any instance of an EventDispatcher subclass to register event listeners. EventDispatcher also defines a protected method called dispatchEvent( ), which you can call from within a subclass to dispatch an event. The dispatchEvent( ) method requires at least one parameter as a flash.events.Event object or a subclass of Event.

Skip to any available Digital Media Help Center chapter of ActionScript 3.0 Cookbook:
Chapter 1 | Chapter 2 | Chapter 3 | Chapter 5 | Chapter 6

Joey Lott is a founding partner of The Morphic Group. He has written many books on Flex and Flash-related technologies, including Programming Flex 3, ActionScript 3 Cookbook, Adobe AIR in Action, and Advanced ActionScript 3 with Design Patterns.
Keith Peters is currently working full time doing freelance and contract Flash development and various writing projects.
Darron Schall has a BS in Computer Science from Lehigh University. Darron is an independent consultant specializing in Rich Internet Applications and Flash Platform development. He maintains a Flash Platform related weblog at http://www.darronschall.com and is an active voice in the Flash and Flex communities.