Listeners


Listeners are a different kind of event handler. Instead of overriding a method of the object, listeners "register" themselves with the object. Then when the event occurs, the object is responsible for notifying all the registered listeners by calling specific methods on the listener objects.

The main advantage of listeners over traditional event handlers is that more than one object can listen for a specific event. It supports the idea of "broadcasting" the eventone object trigging an event handler on multiple listening objects.

Many of the built-in classes support listeners such as the following: Key, Mouse, Stage, MovieClipLoader, FileReference, and so on.

In the following example, we will listen for an onResize event on the Stage class:

   function onResize() {      trace(Stage.width + " x " + Stage.height);    }    Stage.addListener(this);


In this simple example we are creating a function on our main Timeline called onResize. This is the name of the method the Stage class will attempt to call on all registered listeners whenever the stage size is changed.

Inside this listener function we are simply outputting the new dimensions of the stage using the trace command.

The final thing we do is register our Timeline as a listener to the Stage class using the addListener method. If we want to unregister our Timeline, we could use the removeListener method. In the following example, we will remove the listener after the first resize of the stage, which ensures that the stage is resized only once:

   function onResize() {      trace(Stage.width + " x " + Stage.height);      Stage.removeListener(this);    }    Stage.addListener(this);


There are many built-in classes that use listeners. The following is a list of all those classes in Flash 8:

Class

Listeners

flash.net.FileReference

onCancel

 

onComplete

 

onIOError

 

onHTTPError

 

onOpen

 

onProgress

 

onSecurityError

 

onSelect

flash.net.FileReferenceList

onCancel

 

onSelect

System.IME

onIMEComposition

Key

onKeyDown

 

onKeyUp

Mouse

onMouseDown

 

onMouseMove

 

onMouseUp

 

onMouseWheel

MovieClipLoader

onLoadComplete

 

onLoadError

 

onLoadInt

 

onLoadProgress

 

onLoadStart

Selection

onSetFocus

Stage

onResize

TextField

onChanged

 

onScroller





Macromedia Flash 8 ActionScript Training from the Source
Macromedia Flash 8 ActionScript: Training from the Source
ISBN: 0321336194
EAN: 2147483647
Year: 2007
Pages: 221

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