Advanced Event Handling


When you press a mouse button, that's an event. When an image finishes loading onto the stage, that's an event. An event handler triggers actions the programmer wants to occur when an event occurs during run-time. Chapter 11 explored simple event handlers for Button and MovieClip objects. Now, as you explore class-based logic, it's a good idea to look at object listeners and ways to extend the functionality of the built-in event handlers for MovieClip objects.

System Events and User Input Events

You will normally encounter two main types of events in Flash: system events and user input events. System events are generated by the Flash movie and include events such as when data finishes loading or when the movie playhead reaches a particular frame. User input events are generated by the user, such as when the user releases the mouse or a key.

User input events can be "listened" to and acted upon in ActionScript. Several objects can have listeners: Key, Mouse, MovieClipLoader, Selection, Stage, and TextField. An advantage to using listeners is that you can add multiple listeners to the same event.

To use a listener, you must first define a method that will be called when the listener object detects an event. Next, you assign the method to the event handler for that object. And finally, you add the listener to an object that has listener event handlers. What this does is extend the types of events to which a particular object can respond.

For example, imagine that you want the user to use the keyboard to control the movement of a spaceship in your movie. The spaceship instance, which is an instance of the MovieClip class, needs to "listen" to the keyboard and respond to an event generated by the user with the keyboard. To do this, you need to register the spaceship movie clip as a listener for the Key class, as shown in the following example:

function myOnKeyUp(){     trace("key up"); } function myOnKeyDown(){     trace("key down"); } spaceship.onKeyUp = myOnKeyUp; spaceship.onKeyDown = myOnKeyDown; Key.addListener(spaceship);


Scoping and the this Keyword with Event Handlers

You can make your code more general and reusable by using the this keyword. It refers to the object or movie clip instance that contains it. When a method containing this is called, a reference to the object that contains the method is saved in this.

It's especially useful when dynamically attaching functions to a movie clip. Imagine that you want to make your spaceship move 10 pixels per frame while a key on the keyboard is pressed. Because the myOnKeyDown function has been attached to the spaceship movie clip, myOnKeyDown is a method of the spaceship object and this refers to the spaceship. You can add this._x +=10; to that function to have it execute when the user holds the key down, as shown in the following example:

function myOnKeyUp(){     trace("key up"); } function myOnKeyDown(){     trace("key down");     this._x +=10; } spaceship.onKeyUp = myOnKeyUp; spaceship.onKeyDown = myOnKeyDown; Key.addListener(spaceship);


Disabling and Deleting Event Handlers

If you want the spaceship to stop moving when it reaches a certain position on the stage, you can add a conditional to the myOnKeyDown function that tests whether the spaceship movie clip's x position is less than or equal to 300. If the condition is met, the spaceship keeps moving while the key is pressed. However, if the spaceship reaches its destination, its onKeyDown method is deleted and it no longer responds to the keyboard and stops moving.

function myOnKeyUp(){     trace("key up"); } function myOnKeyDown(){     trace("key down");     if(this._x <= 300){         this._x +=10;     } else {         delete this.onKeyDown;     } } spaceship.onKeyUp = myOnKeyUp; spaceship.onKeyDown = myOnKeyDown; Key.addListener(spaceship);


Another way to stop the spaceship from moving is to remove its listener, as shown in this example:

function myOnKeyDown(){     trace("key down");     if(this._x <= 300){         this._x +=10;     } else {         Key.removeListener(this);     } }




Special Edition Using Macromedia Studio 8
Special Edition Using Macromedia Studio 8
ISBN: 0789733854
EAN: 2147483647
Year: 2003
Pages: 337

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