Dispatching events

If you want your component to broadcast events other than the events it may inherit from a parent class, you must call the dispatchEvent() method in the component's class file.

The dispatchEvent() method is defined in the mx.events.EventDispatcher class and is inherited by all components that extend UIObject. (See EventDispatcher class in ActionScript 2.0 Components Language Reference.)

You should also add an Event metadata tag at the top of the class file for each new event. For more information, see About the Event tag.

NOTE

 

For information about handling component events in a Flash application, see Handling Component Events.

Using the dispatchEvent() method

In the body of your component's ActionScript class file, you broadcast events using the dispatchEvent() method. The dispatchEvent() method has the following syntax:

dispatchEvent(eventObj)

The eventObj parameter is an ActionScript object that describes the event (see the example later in this section).

You must declare the dispatchEvent() method in your code before you call it, as follows:

private var dispatchEvent:Function;

You must also create an event object to pass to dispatchEvent(). The event object contains information about the event that the listener can use to handler the event.

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 the target 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, because it is implicit.

The description of each event in the Flash 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 ActionScript 2.0 Components Language Reference.

Common events

The following table lists the common events that are broadcast by various classes. Every component should broadcast these events if they make sense for that component. This is not a complete list of events for all components, just ones that are likely to be reused by other components. Even though some events specify no parameters, all events have an implicit parameter: a reference to the object broadcasting the event.

Event

Use

click

Used by the Button component, or whenever a mouse click has no other meaning.

change

Used by List, ComboBox, and other text-entry components.

scroll

Used by ScrollBar and other controls that cause scrolling (scroll "bumpers" on a scrolling pop-up menu).

In addition, because of inheritance from the base classes, all components broadcast the following events:

UIComponent event

Description

load

The component is creating or loading its subobjects.

unload

The component is unloading its subobjects.

focusIn

The component now has the input focus. Some HTML-equivalent components (ListBox, ComboBox, Button, Text) might also broadcast focus, but all broadcast DOMFocusIn.

focusOut

The component has lost the input focus.

move

The component has been moved to a new location.

resize

The component has been resized.

The following table describes common key events:

Key events

Description

keyDown

A key is pressed. The code property contains the key code and the ascii property contains the ASCII code of the key pressed. Do not check with the low-level Key object, because the event might not have been generated by the Key object.

keyUp

A key is released.


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00002514.html