Using the event object

An event object is passed to a listener as a parameter. The event object is an ActionScript object whose properties contain information about the event that occurred. You can use the event object in the listener callback function to access the name of the event that was broadcast, or the instance name of the component that broadcast the event.

For example, the following code uses the target property of the evtObj event object to access the label property of the myButton instance and trace the value:

listener = new Object();
listener.click = function(evtObj){
  trace("The " + evtObj.target.label + " button was clicked");
}
myButton.addEventListener("click", listener);

The following table lists the properties that are common to every event object:

Property

Description

type

A string that indicates the name of the event. This property is required.

target

A reference to the component instance that is broadcasting the event. In general, you are not required to describe this reference object explicitly.

The most common events, such as click and change, have no required properties other than type.

You can explicitly build an event object before dispatching the event, as the following example shows:

var eventObj = new Object();
eventObj.type = "myEvent";
eventObj.target = this;
dispatchEvent(eventObj);

You can also use a shortcut syntax that sets the value of the type property and dispatches the event in a single line:

ancestorSlide.dispatchEvent({type:"revealChild", target:this});

In the preceding example, setting the target property is optional, since it is implicit.

The description of each event in the Flash MX 2004 documentation lists the event properties that are optional and required. For example, the ScrollBar.scroll event takes a detail property in addition to the type and target properties. For more information, see the event descriptions in Components Dictionary.