Chapter 4 Handling Events; Using Event Handler Methods - MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT Reference Manual

Actionscript reference guide
Hide thumbs Also See for FLASH MX 2004 - ACTIONSCRIPT:
Table of Contents

Advertisement

CHAPTER 4
Handling Events
An event is a software or hardware occurrence that requires a response from a Macromedia Flash
application. For example, an event such as a mouse click or a keypress is called a user event, since it
occurs as a result of direct user interaction. An event generated automatically by Flash Player, such
as the initial appearance of a movie clip on the Stage, is called a system event, because it isn't
generated directly by the user.
In order for your application to react to events, you must use event handlers—ActionScript code
associated with a particular object and event. For example, when a user clicks a button on the
Stage, you might advance the playhead to the next frame. Or when an XML file finishes loading
over the network, you might display the contents of that file in a text field.
ActionScript provides a few different ways to handle events: event handler methods, event
listeners, and button and movie clip event handlers.

Using event handler methods

An event handler method is a class method that is invoked when an event occurs on an instance of
that class. For example, the Button class defines an
event handler that is invoked
onPress
whenever the mouse is pressed on a Button 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.
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
}
83

Advertisement

Table of Contents
loading

Table of Contents