ActionScript 3.0 Cookbook: Chapter 2, Custom Classes
Pages: 1, 2, 3, 4
You want to declare a constant.
Declare it just like you would declare a property, except use
the const keyword in place of var.
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.
Recipe 2.4
You want to dispatch events.
Extend flash.events.EventDispatcher and call the dispatchEvent( ) method.
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.
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: |