USING LISTENERS


USING 'LISTENERS'

Just as a single transmitter broadcasts radio waves to thousands of radio receivers, events can be "broadcast" to any number of objects so that they respond accordingly. You do this by employing what are known as listeners events that can be broadcast to objects that have been "registered" to listen for them (listening objects).

Suppose you wanted an object (sound, movie clip, text field, array, custom) to react to a certain event being triggered in your movie. You would first create an event handler method for that object:

 myTextField.onMouseDown = function(){    myTextField.text="";  } 

Here we've set up a text field named myTextfield to react when the mouse is pressed down. The onMouseDown event is not a standard event that a text field can react to, and at this point it still won't. In order for the text field to react to this event, you must register the text field as a listener for the event. You can do that by adding the following code:

 Mouse.addListener("myTextField"); 

Here the text field is registered with the Mouse object, since the onMouseDown event is a listener of that object. Now, whenever the mouse is pressed down, the event handler method we created earlier will execute. You can set up any number of objects to a single listener event as long as the object has been given an event handler method to deal with the event, and it has been registered to listen for that event. If you no longer want an object to listen for an event, you can unregister it using the following syntax:

 Mouse.removeListener("myTextField"): 

Listeners just give you a bit more flexibility for scripting your projects to react to various events.

NOTE

Not all events can be treated as listeners. With this in mind, be aware of which events are listed as listeners in the Actions toolbox. If an event is not listed as a listener, other objects can not be set up to listen to it.




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