« Cool design always beats cool technology | Main | Flash 8 now shipping.... »

Flash 8 and AsBroadcaster

The previously undocumented AsBroadcaster listener event system is now part of Flash 8... but do we really need it when we've rolled our own in Flash 7 (and the home grown version is a mellower smoke)?

Note: this post assumes a good understanding of ActionScript2.0 class based coding.

One of the coolest 'cool but undocumented' features of Flash 7 (a.k.a MX 2004) was AsBroadcaster. This useful class allows your custom classes to pass events between each other, something that you need when creating complex class based structures

For example, in web applications, you will want to create a class that maintains the underlying webservice as well as creating an event handling model that allows the rest of your code to respond automatically to service traffic. To do this effectively, you need to define custom event handling within your classes, and this is what AsBroadcaster allows you to set up.

However, Flash 7 AsBroadcaster sufferred from two problems.

Firstly, it was undocumented, so your boss wouldn't like you to use it, just in case it got changed in Flash 8 (and all your applications stopped working). Now that AsBroadcaster is documented, this is not an issue, but the undocumented status most likely prevented any real serious use in Flash 7.

Secondly, and more subtly (i.e - you won't realise this until you actually try to use Asbroadcaster a few times...), you can't use dynamic classes (such as AsBroadcaster) in static classes. The problem here is that some of the most likely classes that would benefit from being broadcasters are the base (and invariably) static classes that underpin the sort of complex system where you would want to use a broadcaster-listener system in the first place...

... which is why, in Flash 7, I deconstructed AsBroadcaster and created my own simple broadcast methods. Once you do this, it becomes apparent that broadcaster-listener systems are very simple in their construction.

All a broadcaster-listener event system consists of is a list of registered listeners, held by the broadcaster. This list is nothing more than an array of method (Function) references. When the broadcaster broadcasts an event, all it actually does is cycle through the listener list, invoking all the method references.

This is easily created in your own classes without ever needing to use AsBroadcaster at all. Your addListener method is simply a 'push to array', and the broadcast method just cycles through the array, calling the broadcast method for each registered object.

Here's an example (highly simplified) broadcasting class:


class CBroadcast {
  //  CBroadcaster class definition
  var m_listeners:Array;
  //
  function CBroadcast() {
    m_listeners = new Array();
  }
  //
  public function addListener(anObj:Object):Void {
    //  This function registers a listener to CBroadcast.
    // 
    m_listeners.push(anObj);
  }
  //
  public function broadcast(msg:String):Void {
    //  This function broadcasts to all registered listeners.
    //  If a listener (say aListener) has the function aListener.msg, 
    //  that function will be executed.
    //
    for (var i = 0; i<m_listeners.length; i++) {
      m_listeners[i][msg]();
    }
  }
  //
}

Here's a corresponding listening class:


class CListen {
  //  CListen class definition
  //
  function CListen() {
  }
  //
  public function anEvent():Void {
    trace("CListen: I heard the anEvent event.");
  }
  //
}

Assuming you have saved these two classes as Cbroadcast.as and CListen.as, you can test your custom broadcaster-listener event model with the following FLA code:


var aBroadcaster:CBroadcast = new CBroadcast();
var aListener1:CListen = new CListen();
var aListener2:CListen = new CListen();
aBroadcaster.addListener(aListener1);
aBroadcaster.addListener(aListener2);
aBroadcaster.broadcast("anEvent");

Your CListen instances will both execute the CListen.anEvent method (event) when the CBroadcast class calls Cbroadcast.broadcast("anEvent").

This is a powerful way to add a custom event model to your classes, and works even when your classes are static. Further, it works in both Flash 8 and Flash 7 - by 'works' we mean 'your boss would allow you to use it, even in Flash 7'.

Bonus!

Posted by motiongraphics on September 10, 2005 02:52 AM

Comments
Comment by Dwayne Neckles

So how do I use this for visual effects

Posted on December 15, 2006 10:14 PM

Post a comment




Remember Me?

(you may use HTML tags for style)