Understanding Event Handler Methods


Although we've discussed a number of events thus far, we've really just scratched the surface of Flash reactions. You already know that scripts react to all kinds of triggers: interaction with the mouse, the timeline's reaching a specific frame, movie clip instances entering a scene. But did you 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, this script is attached to a movie clip instance named myMovieClip_mc and is executed whenever the mouse button is pressed while the instance is present in the scene:

 onClipEvent(mouseDown) {   this._rotation = 45; } 

This script will rotate the movie clip instance by 45 degrees when the mouse button is pressed.

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

 myMovieClip_mc.onMouseDown = function() {   this._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_mc) from the event to which it needs to react. And to reiterate, we've placed this script on a frame rather than attached it to the instance.

Don't worry about the use of function() in the script. We'll provide an in-depth discussion of functions in Lesson 5, "Using Functions." 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.

NOTE

To execute a particular function when an event handler method is defined, change the syntax to myMovieClip.onMouseDown = nameOfFunction;.


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

Because this script describes how the myMovieClip_mc 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 from regular events, and if there are any advantages of using one over the other. That's what we'll discuss next.

NOTE

Event handler methods play a large role in the way custom objects are set up to react to events. For more information, see Lesson 7, "Creating Custom Classes."




Macromedia Flash MX 2004 ActionScript(c) Training from the Source
Macromedia Flash MX 2004 ActionScript: Training from the Source
ISBN: 0321213432
EAN: 2147483647
Year: 2005
Pages: 182

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