User Guide

370 Handling Events
* @param message The text to display when the alarm goes off.
*/
public function AlarmEvent(message:String = "ALARM!")
{
super(ALARM);
this.message = message;
}
...
}
The best way to create a custom event object class is to define a class that extends the Event
class, as shown in the preceding example. To supplement the inherited functionality, the
AlarmEvent class defines a property
message that contains the text of the alarm message
associated with the event; the
message value is passed in as a parameter in the AlarmEvent
constructor. The AlarmEvent class also defines the constant
ALARM, which can be used to refer
to the specific event (
alarm) when calling the AlarmClock classs addEventListener()
method.
In addition to adding custom functionality, every Event subclass must override the inherited
clone() method as part of the ActionScript event handling framework. Event subclasses can
also optionally override the inherited
toString() method to include the custom events
properties in the value returned when the
toString() method is called.
/**
* Creates and returns a copy of the current instance.
* @return A copy of the current instance.
*/
public override function clone():Event
{
return new AlarmEvent(message);
}
/**
* Returns a String containing all the properties of the current
* instance.
* @return A string representation of the current instance.
*/
public override function toString():String
{
return formatToString("AlarmEvent", "type", "bubbles", "cancelable",
"eventPhase", "message");
}
The overridden clone() method needs to return a new instance of the custom Event subclass,
with all the custom properties set to match the current instance. In the overridden
toString() method, the utility method formatToString() (inherited from Event) is used
to provide a string with the name of the custom type, as well as the names and values of all its
properties.