330
Handling Events
For more information on handling events, see the following sections:
Using event handler methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Using event listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .332
Using event listeners with components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .335
Using button and movie clip event handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337
Broadcasting events from component instances . . . . . . . . . . . . . . . . . . . . . . . . . . . .342
Creating movie clips with button states . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .342
Event handler scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .343
Scope of the this keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347
Using the Delegate class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347
Using event handler methods
An event handler method is a method of a class that is invoked when an event occurs on an
instance of that class. For example, the MovieClip class defines an
onPress
event handler that
is invoked whenever the mouse is pressed on a movie clip object. Unlike other methods of a
class, however, you don’t invoke an event handler directly; Flash Player invokes it
automatically when the appropriate event occurs.
The following ActionScript classes are examples of classes that define event handlers: Button,
ContextMenu, ContextMenuItem, Key, LoadVars, LocalConnection, Mouse, MovieClip,
MovieClipLoader, Selection, SharedObject, Sound, Stage, TextField, XML and XMLSocket.
For more information about the event handlers they provide, see the entries for each class
in
ActionScript 2.0 Language Reference
. The word
handler
is added in the title of each
event handler.
By default, event handler methods are undefined: when a particular event occurs, its
corresponding event handler is invoked, but your application doesn’t respond further to the
event. To have your application respond to the event, you define a function with the function
statement and then assign that function to the appropriate event handler. The function you
assign to the event handler is then automatically invoked whenever the event occurs.
An event handler consists of three parts: the object to which the event applies, the name of the
object’s event handler method, and the function you assign to the event handler. The
following example shows the basic structure of an event handler:
object
.
eventMethod
= function () {
// Your code here, responding to event.
}
Summary of Contents for FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Page 1: ...Learning ActionScript 2 0 in Flash...
Page 8: ...8 Contents...
Page 18: ...18 Introduction...
Page 30: ...30 What s New in Flash 8 ActionScript...
Page 66: ...66 Writing and Editing ActionScript 2 0...
Page 328: ...328 Interfaces...
Page 350: ...350 Handling Events...
Page 590: ...590 Creating Interaction with ActionScript...
Page 710: ...710 Understanding Security...
Page 730: ...730 Debugging Applications...
Page 780: ...780 Deprecated Flash 4 operators...
Page 830: ...830 Index...