Macromedia flex 2 Manuale

Pagina di 254
42
Creating Custom Events
eventObj.isEnabled=true;
dispatchEvent(eventObj);
For complete examples that create and dispatch custom events, se
 and 
.
Creating static constants for the Event.type property
The constructor of an event class typically takes a single required argument that specifies the 
value of the event object’s 
type
 property. In the previous section, you passed the string 
enableChange
 to the constructor, as the following example shows:
// Define event object, initialize it, then dispatch it. 
var eventObj:EnableChangeEvent = new EnableChangeEvent("enableChange");
dispatchEvent(eventObj);
The Flex compiler does not examine the string passed to the constructor to determine if it is 
valid. Therefore, the following code compiles, even though 
enableChangeAgain
 might not 
be a valid value for the 
type
 property:
var eventObj:EnableChangeEvent = 
new EnableChangeEvent("enableChangeAgain");
Because the compiler does not check the value of the 
type
 property, the only time that your 
application can determine if 
enableChangeAgain
 is valid is at run time. 
However, to ensure that the value of the 
type
 property is valid at compile time, Flex event 
classes define static constants for the possible values for the 
type
 property. For example, the 
Flex 
EffectEvent
 class defines the following static constant:
// Define static constant for event type.
public static const EFFECT_END:String = "effectEnd";
To create an instance of an EffectEvent class, you use the following constructor:
var eventObj:EffectEvent = new EffectEvent(EffectEvent.EFFECT_END);
If you incorrectly reference the constant in the constructor, the compiler generates a syntax 
error because it cannot locate the associated constant. For example, the following constructor 
generates a syntax error at compile time because 
MY_EFFECT_END
 is not a predefined constant 
of the EffectEvent class:
var eventObj:EffectEvent = new EffectEvent(EffectEvent.MY_EFFECT_END);