Listeners and AsBroadcaster
         by senocular

Introduction
In this section, briefly explain what the tutorial will explain/accomplish. If you want, you can include something "dramatic" that gets readers interested in your topic. Try to enjoy writing tutorials and follow the basic guidelines found below. Before I forget, feel free to modify this document as much as needed.

Extending What Was
Buttons are a good example to start with. The entire concept of a button revolves around user interaction, typically that of pressing. When you press a button, you expect certain results to follow, and when properly coded, they do. What determines when these events occur is the button's ability to detect or listen for the user interaction. When the pressing of the button is detected or broadcasted by the flash player, the on(press) event in that button is handled and the containing code executed. Other events buttons listen for are release, rollOver, etc., while Flash movieclips similarly listener for events such as enterFrame, mouseMove, keyDown, etc.

Prior to Flash MX, these listener objects (buttons and movieclips) were pre-defined by Flash and could not be altered, nor new ones created, by the author of the movie. MX has changed all that with the new object model. Buttons and movieclips are still pre-defined, but now you can have your very own custom objects listen for certain events like those recognized by buttons and movieclips as they occur in Flash. Of course these custom objects are not restricted to the predefined events such as keyDown or mouseMove. You have the option of making your own events with the use of AsBroadcaster (Action Script Broadcaster).

So what kinds of events, or broadcasters, are built into Flash? Well, these are

Event Broadcaster Handled by (Listeners)
 onDragOut  -  Button, MovieClip
 onDragOver  -  Button, MovieClip
 onKeyDown  Key  Button, MovieClip
 onKeyUp  Key  Button, MovieClip
 onKillFocus  -  Button, MovieClip, TextField
 onPress  -  Button, MovieClip
 onRelease  -  Button, MovieClip
 onReleaseOutside  -  Button, MovieClip
 onRollOut  -  Button, MovieClip
 onRollOver  -  Button, MovieClip
 onSetFocus  Selection  Button, MovieClip, Selection, TextField
 onData  -  MovieClip, LoadVars, XML
 onEnterFrame  -  MovieClip
 onLoad  -  MovieClip, LoadVars, Sound, XML
 onMouseDown  Mouse  MovieClip
 onMouseMove  Mouse  MovieClip
 onMouseUp  Mouse  MovieClip
 onUnload  -  MovieClip
 onResize  Stage  Stage
 onChanged  TextField  TextField
 onScroller  TextField  TextField

You'll notice that not all of the events, like onDragOut, are associated with a broadcaster object. You can consider these to be broadcasted by the Flash player itself rather than through an actionscript object within Flash. These events are restricted to being used only by whatever objects can handle them, as listed. onDragOut, for example, being only available for use with a Button or MovieClip object. The ones with broadcasters allow you to add your own listeners to these objects allowing those listeners to recognize when an event is initiated. For example, you could make a generic object in actionscipt and have it listen to the Mouse object (a broadcaster) which will then allow that object to run onMouseDown, onMouseMove, and onMouseUp code whenever the mouse is pressed, moves or is released.

There are two different ways to look at listeners and broadcasters.to really understand how they work. The more literal sense revolves around focusing on the individual listeners themselves and how they can become “aware” and listen for events occuring in Flash. The other is based more on how listeners and AsBroadcaster works internally in Flash.

Listeners Listen
One of my first usages of listeners was getting a movieclip in MX to recognize an onKeyUp event. If an onClipEvent(keyUp) is placed on the movieclip itself, the keyUp event is recognised and my inserted code would run appropriately. Trying to define the event through instance_mc.onKeyUp = function(){...} yielded no result. It wasn't until later I realized that I had to make my movieclip a listener of the Key object so it would know when the key was pressed and would recognise to run the onKeyUp event when a key on the keyboard was released. To do that:

Key.addListener(instance_mc);
instance_mc.onKeyUp= function(){
// key up code
}

Why is a movieclip not automatically a listener of the Key object as it is as an onClipEvent? To be honest, I'm not sure and am too lazy to look it up for the purposes of this article. Its registered as a listener of the Mouse object by default (though thats not entirely true since its not really being handled by the Mouse broadcaster but the Flash player itself, but...) for example instance_mc.onMouseUp = function(){...} works, but not Key.

Nonetheless, once any object, such as my movieclip object, becomes a listener of a broadcasting object, it can then recognize events sent out by that broadcasting object and perform the appropriate actions. From the list we see Flash's broadcasting objects are

Key
Selection
Mouse
Stage
TextField

Each one of these can have listeners added to them using the addListener() method, making the passed object aware of its events. The Selection object, for example, has one event, onSetFocus which is run whenever the focus in Flash changes. When an object is added to the Selection object as a listener (and this can be any object, a generic object, a movieclip, or even a button) it then becomes aware of whenever the Selection object broadcasts that event and runs whatever onSetFocus function assigned to that object.

Example 1
So lets make an object and have it be a listener to the Mouse object. This train of thought is the first I mentioned, the “literal” way of thinking meaning we'll follow the metaphors set aside by Macromedia in understanding how listeners work. That metaphor being that of a listener. A listener is an object that listens for an event to occur and then executes a given script based on what event it was.
 

// make a new generic object
aBigEar = new Object();

 
// define a function to perform a task
Flicked = function(){
trace("Ouch, you just flicked me!");
}

 
// make aBigEar a listener for events from the Mouse object
Mouse.addListener(aBigEar);

 
// assign an onMouseDown event to aBigEar
// since aBigEar is a listener of the Mouse object, it will
// recognise mouse events just as a movieclip would
aBigEar.onMouseDown = Flicked;

You can throw this code right into a new movie and test it out. When you click the mouse, the output window will display “Ouch, you just flicked me!” aBigEar, once made a listener of the Mouse object, runs whatever is assigned to its onMouseDown whenever the mouse is pressed. In this case, the function Flicked is run and the contained trace message is displayed.

onMouseDown itself, to the aBigEar object, is nothing more than just an ordinary function. There's really nothing special about it despite the pretty color-coding given to it by Flash. You could call this function just as easily by saying aBigEar.onMouseDown(). The Mouse object though, has the event onMouseDown associated with it. Because of this, when the aBigEar object is set to listen to the Mouse object, it will hear when that onMouseDown event is triggered and run its own onMouseDown function. AsBroadcaster lets you define your own events, letting you call whatever function you want in your listener object(s).

   

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.