EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Using Event Handler Methods

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
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 dont 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 doesnt 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.
}

Table of Contents

Related product manuals