Using event listeners
85
Using event listeners
Event listeners let an object, called a
listener object
, receive events generated by another object,
called a
broadcaster object
. The broadcaster object registers the listener object to receive events
generated by the broadcaster. For example, you could register a movie clip object to receive
onResize
notifications from the Stage, or a button instance could receive
onChanged
notifications from a text field object. You can register multiple listener objects to receive events
from a single broadcaster, and you can register a single listener object to receive events from
multiple broadcasters.
The event model for event listeners is similar to that of event handlers (see
“Using event handler
methods” on page 83
), with two main differences:
•
The object to which you assign the event handler is not the object that emits the event.
•
You call a special method of the broadcaster object,
addListener()
, which registers the
listener object to receive its events.
To use event listeners, you create a listener object with a property that has the name of the event
being generated by the broadcaster object. You then assign a function to the event listener that
responds in some way to the event. Lastly, you call
addListener()
on the object that’s
broadcasting the event, passing it the name of the listener object. The following code outlines the
event listener model.
listenerObject
.
eventName
= function(){
// your code here
};
broadcastObject
.addListener(
listenerObject
);
The specified listener object can be any object, such as a movie clip or button instance on the
Stage, or an instance of any ActionScript class. The event name is an event that occurs on
broadCastObject
, which then broadcasts the event to
listenerObject
. You can register
multiple listeners to one event broadcaster.
The following example shows how to use the
Selection.onSetFocus
event listener 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 (displayed), and the border of the text field that lost focus
is disabled.
Summary of Contents for FLASH MX 2004 - ACTIONSCRIPT
Page 1: ...ActionScript Reference Guide...
Page 8: ...8 Contents...
Page 12: ......
Page 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Page 54: ...54 Chapter 2 ActionScript Basics...
Page 80: ...80 Chapter 3 Writing and Debugging Scripts...
Page 82: ......
Page 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Page 112: ......
Page 120: ...120 Chapter 6 Using the Built In Classes...
Page 176: ......
Page 192: ...192 Chapter 10 Working with External Data...
Page 202: ...202 Chapter 11 Working with External Media...
Page 204: ......
Page 782: ...782 Chapter 12 ActionScript Dictionary...
Page 793: ...Other keys 793 221 222 Key Key code...
Page 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Page 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Page 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Page 816: ...816 Index...