334 Handling Events
The code starts with an object, listenerObject, with a property eventName. Your listener
object can be any object, such as an existing object, movie clip, or button instance on the
Stage, or it can be an instance of any ActionScript class. For example, a custom movie clip
could implement the listener methods for Stage listeners. You could even have one object that
listens to several types of listeners.
The
eventName property is an event that occurs on broadcasterObject, which then
broadcasts the event to
listenerObject. You can register multiple listeners to one
event broadcaster.
You assign a function to the event listener that responds to the event in some way.
Last, you call the
addListener() method on the broadcaster object, passing the listener
object to the addListener() method.
To unregister a listener object from receiving events, you call the
removeEventListener()
method of the broadcaster object, passing it the name of the event to remove, and the
listener object.
broadcasterObject.removeListener(listenerObject);
Event listener example
The following example shows how to use the onSetFocus event listener in the Selection class
to create a simple focus manager for a group of input text fields. In this case, the border of the
text field that receives keyboard focus is enabled (appears), and the border of the text field that
does not have focus is disabled.
To create a simple focus manager with event listeners:
1. Using the Text tool, create a text field on the Stage.
2. Select the text field, and then in the Property inspector, select Input from the Text Type
pop-up menu and select the Show Border Around Text option.
3. Create another input text field below the first one.
Make sure the Show Border Around Text option is not selected for this text field. You can
continue to create input text fields.
4. Select Frame 1 in the Timeline and open the Actions panel (Window > Actions).