- Effective Communications
- Using AsBroadcaster to Broadcast Custom Messages
- Summary
Using AsBroadcaster to Broadcast Custom Messages
We are not limited to listening to built-in objects. Flash MX 2004 has a singleton class that we can use to allow any Flash object to have listeners subscribe to it. This class is named AsBroadcaster.
NOTE
AsBroadcaster is an undocumented feature of Flash MX 2004. It should be used cautiously; there is no guarantee that it will be implemented in future versions of Flash.
AsBroadcaster essentially has a single method, initialize. This method takes an object as an argument. It returns that object, with three new methods added to it. These new methods are addListener, removeListener, and broadcastMessage. The first two methods act just like the methods of the same names discussed earlier in this article. The third method added with AsBroadcaster is broadcastMessage, which enables an object to send a message that will be captured by any objects listening to it. Listing 2 shows an object being created and initialized as a broadcaster.
Listing 2 Initializing an Object as a Broadcaster
var myBroadcastingObject:Object=new Object(); AsBroadcaster.initialize(myBroadcastingObject);
After an object has been passed to the initialize method of AsBroadcaster, the three methods are automatically associated with it.
Let's consider a slightly more complicated example within the framework of an object-oriented application. Figure 2 shows the Stage for our example. On the Stage, there are four visual elements all contained within a single movie clip. The movie clip has a symbol name mcBroadcast in the Library, and it has an instance name of broadcast_mc. In the Linkage panel for the mcBroadcast clip, this clip is set to be exported for ActionScript, and it is set to implement the ActionScript 2.0 class BroadcastView.
Figure 2 This movie, with a circle, square, text field, and combo box, serves as the framework to demonstrate AsBroadcaster.
Listing 3 shows the source code for BroadcastView.as. In the listing, all the code associated with the application is located in the class files; the code is neither on a frame of the timeline nor directly on the MovieClip objects themselves.
Listing 3 BroadcastView Defines the Behaviors for Our Movie Clip
class BroadcastView extends MovieClip{ // declare visual objects var combo:mx.controls.ComboBox; var rec_mc:MovieClip; var oval_mc:MovieClip; var text_mc:MovieClip; // declare properties of class var colorsArray:Array; var addListener:Function; var value:Number; // constructor public function BroadcastView (){ colorsArray = this.buildArray(); } function onEnterFrame(){ this.postInit(); } function postInit(){ this.fillComboBox(this.colorsArray); this.combo.addEventListener("change",this.changeColor); AsBroadcaster.initialize(this); this.addListener(oval_mc); this.addListener(rec_mc); this.addListener(text_mc); this.onEnterFrame = null; } private function buildArray():Array{ var theArray:Array= new Array(); theArray.push(new TheColor("Red",0xff0000)); theArray.push(new TheColor("Blue",0x0000ff)); theArray.push(new TheColor("Green",0x00ff00)); theArray.push(new TheColor("Yellow",0xffff00)); theArray.push(new TheColor("Pink",0xff00ff)); theArray.push(new TheColor("Cyan",0x00ffff)); return theArray; } private function fillComboBox(colorsArray){ for(var i:Number=0;i<colorsArray.length;i++){ this.combo.addItem( colorsArray[i].colorName, colorsArray[i].hexColor ); } } public function changeColor(){ this._parent.broadcastMessage("changeColor",this.value); } }
NOTE
Notice that the changeColor method uses this._parent as the scope when it invokes the broadcastMessage method. It does this because a function or method acting as an event handler for a component is invoked within the scope of the component rather than the scope of the class, as was mentioned in a Tip earlier this article. This method broadcasts the selected color to all the listening objects.
Listing 4 shows the source for the class file TheColor. This class simply has two properties, colorName and hexColor, a constructor, and some simple getter and setter methods. The buildArray method from the BroadcastView class uses the TheColor class to assemble an array of color names and values.
Listing 4 The Class File for TheColor Defines How Instances of TheColor Are Built
class TheColor{ var colorName:String; var hexColor:Number; function TheColor(name:String,hex:Number){ this.setName(name); this.setHex(hex); } private function setName(name:String):Void{ this.colorName = name; } private function setHex(hex:Number):Void{ this.hexColor = hex; } public function getName():String{ return this.colorName; } public function getHex():Number{ return this.hexColor; } }
The other visual objects on the Stage each have class files associated with them. These other classes simply define a changeColor method for the class, allowing them to respond to the changeColor message being broadcast.
Both mcOval and mcRec (the Library items associated with oval_mc and rec_mc) are defined through the Linkage panel as implementing the ActionScript 2.0 class ColorChanger. Listing 5 shows the implementation of the ColorChanger class.
Listing 5 The ColorChanger Class has a Single Method That Fires When the changeColor Message Is Broadcast to It
class ColorChanger extends MovieClip{ function changeColor(color:Number):Void{ var newColor = new Color(this); newColor.setRGB(color); delete newColor; } }
Finally, the mcText symbol (the Library item associated with text_mc) is defined through the Linkage panel as implementing the ActionScript 2.0 class TextMovie. Listing 6 shows the class definition for TextMovie.
Listing 6 TextMovie Has a Single MethodchangeColorThat Fires When the changeColor Message Is Broadcast to It
class TextMovie extends MovieClip{ var dynTxt:TextField; public function changeColor(color:Number):Void{ DynText.textColor=color; } }
With the classes in this example, we can see how custom messages can be broadcast with the use of AsBroadcaster. A single message is broadcast to three separate objects, implementing two independent classes. Each class has its own individual method for handling the message. Figure 3 shows the application running.
Figure 3 When the application is run, the text and two shapes change color to match the color chosen from the combo box.