12.10. Listening for Events
Problem
You want to perform an action in response to an event, such as when the Stage is resized, a text field’s contents are scrolled, the mouse button is clicked, or a key is pressed.
Solution
Create a listener object that defines
an appropriate listener event handler and
add it as a listener using addListener( )
.
Discussion
ActionScript’s listener events
allow one or more objects to respond to events that occur for other
objects. A listener object
(an
object that has been added as a
listener) is notified when one of the supported listener events
occurs. For example, the built-in Key
,
Mouse
, Selection
, and
Stage
objects generate one or more listener
events, as do instances of the TextField
class,
as
shown in Table 12-1.
Table 12-1. Listener events for built-in objects and instances
Object |
Built-in listener events |
---|---|
|
onKeyDown onKeyUp |
|
onMouseDown onMouseMove onMouseUp |
|
onSetFocus |
|
onResize |
|
onChanged onScroller |
A listener object can be any kind of object, such as a text field,
movie clip, or custom object. The listener object must define a
listener event handler to respond to the event of interest, and it
must be added as a listener using addListener(
)
. For example, to respond to text-scrolling events, you
can create an object that defines an onScroller(
)
method and then register the object to the text field of
interest using the addListener( )
method. ...
Get Actionscript Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.