Although we've discussed a number of events thus far, we've really just scratched the surface of the things Flash can react to. You already know that scripts react to all kinds of triggers: interaction with the mouse, the timeline reaching a specific frame, movie clip instances entering a scene. But did you also know that by using event handler methods, you can make your scripts execute when sounds finish playing, when the stage is resized, or when the text in a text field changes? You can even use event handler methods to extend the functionality of the events we've already used in this lesson.

Although event handler methods and standard event handlers are used for the same basic purpose (that is, executing a script when something happens in your movie), you must implement them a bit differently.

By now, you know how to set up a script to be executed as the result of an event. For example, the following script is attached to a movie clip instance named myMovieClip and is executed whenever the mouse is pressed down while the instance is present in the scene:

 onClipEvent(mouseDown) {    _rotation = 45;  } 

This script will rotate that instance by 45 degrees when the mouse is pressed down.

Using an event handler method, the following script would be placed on a frame of the timeline to accomplish the same purpose, rotating myMovieClip whenever the mouse is pressed down.

 myMovieClip.onMouseDown = function() {    myMovieClip._rotation = 45;  } 

Instead of using onClipEvent to define the event handler (as shown in the first script), here we use a dot (.) to separate the name of the object (in this case myMovieClip) from the event to which it needs to react. And to reiterate, we've placed this script on a frame rather than attach it to the instance.

Don't worry about the use of function() in the above script. We'll provide an in-depth discussion of functions in Chapter 5. All you need to know about this use of function() is that it's a necessary part of the syntax for implementing the event handler method, as shown.


Once you become familiar with functions, if you would like a particular one to be executed when defining an event handler method, you would change the above syntax in the following manner: myMovieClip.onMouseDown = nameOfFunction;

The actions in the second line of the script (between curly braces) define what needs to happen when the event occurs.


You'll notice that in the first script, no target path is defined (_rotation = 45 ), but that in the second a target path is defined (myMovieClip._rotation = 45 ). Since the first script is attached to myMovieClip, no target path is necessary that is, the target is understood to be the movie clip itself. However, because the second script is on a frame, you need a path to identify the object to be rotated. When using event handler methods as described here, you need to be aware of the target paths in the triggered actions. For more on target paths, see Lesson 3, Understanding Target Paths.

Since this script describes how the myMovieClip instance reacts to the onMouseDown event, that instance must exist in the scene at the time the event handler method is defined. This will attach the defined functionality to the instance. By the same token, event handler methods assigned to objects are removed when the object leaves the scene (or is otherwise removed). If the object appears in the scene again, any event handler methods will need to be defined again.

At first glance, you may be wondering how event handler methods are much different than the regular events, and if there are any advantages of using one over the other. That's what we'll discuss next.


Event handler methods play a large role in the way custom objects are set up to react to events. For more information, see Lesson 6, Customizing Objects.

Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net