| Register
Saturday, May 17, 2008   

Events: Broadcasting, Listening, and Responding

Created By  Jed Wood, at  8/2/2004 - 2 comments.

Click to view this author's website.

Intro

UPDATE:

http://www.moock.org/blog/archives/000134.html

Despite jumping right onto the Flash MX bandwagon and dumping Flash 5 as fast as I could, I've been much slower in joining the throngs of OO-loving AS 2.0 users. Based on various blog discussions/debates, I know I'm not the only one. Part of that has to do with what I spend most of my day doing at my current job: making rapid prototypes for testing interaction and usability. It's hard to beat some good old AS 1.0 scattered all over the place when you know that the interesting new interface you're working on has a life span of about 6 hours.

But finally, I'm trying my best to catch up. I even received my copy of http://www.moock.org/eas2/ in the mail yesterday. In my sleep I mumble about encapsulation. modularity. portability. flexibility. reusability. extensibility. Soon, I might even be able to hang out with the Java developers in the cubicle next to me.

It all makes sense, until I hit the point where I actually start putting things together. I fight the urge to go back to my evil procedural ways-- or even the old prototype-based pseudo OO system I grew to love with AS 1.0, but then I worry that someone somewhere someday might look at my code-- and then they'll know!

I know, I know-- design patterns are the answer. And I'm trying to become familiar with more of them. But in the meantime, I'd like to gather your opinions, links and tidbits on this whole Event thing. Hopefully I, and all the other hesitant AS 2.0 users, will do start doing things right.

Our Example

We're going to click on an object, and that same object is going to fade out to nothingness. It will also trace something out to the Output window. Originally, I did things like this:

ClickMeClass.prototype.onRelease = function() {
Trace ("wooho");
_global.fader(this, "out");
}

But, that meant stacking my code within that one callback (whether calling other functions or carrying out actions more directly). The solution I then started using was Branden Hall's EventBroadcaster class, which takes advantage of ASBroadcaster (see page 48 of http://www.wheelmaker.org/). With that, I could just broadcast a message out to the world (and even pass arguments with that broadcast), and add/remove listeners whenever I wanted, like using Stage or Mouse. Sample:

//The Class "constructor"
ClickMe = function() {
//define a property
this.msg = "Woohoo!";
EventBroadcaster.initialize(this);
//add listeners. This of course could also be done
//for individual instances instead of on the protoype
ClickMe.addListener(Fader);
ClickMe.addListener(Tracer);
}

ClickMe.protoype.onRelease = function() {
//broadcast a message, and pass a reference back to this instance
this.broadcastMessage("onClick", this);}


//define the onClick for the listener
Fader.onClick = function(obj) {
// code to fade obj
}

Tracer.onClick = function(obj) {
Trace (obj.msg);
}

Along comes AS 2.0

This is where things get interesting. With the release of AS 2.0, it seemed the recommended way to handle events was quite similar to the Hall system above. You set up your objects to broadcast an event, and add/remove listeners as you please. But the experts started finding disadvantages with that system.

  1. http://www.philterdesign.com/blog/archives/000013.html
  2. A few days later, http://www.gskinner.com/blog/archives/000027.html
  3. A couple months later, http://chattyfig.figleaf.com/ezmlm/ezmlm-cgi/1/98729
  4. That prompted Mike Chambers to release http://www.markme.com/mesh/archives/004286.cfm a month later
  5. In March, Darron Schall did a http://www.darronschall.com/weblog/archives/000100.cfm
  6. In Moock's book (http://www.macromedia.com/devnet/mx/flash/articles/comp_moock.html), he alters Mike's EventProxy slightly.
  7. Flash 7.2 updater adds the official Delegate, http://www.macromedia.com/devnet/mx/flash/articles/eventproxy.html

And of course, most of those articles have their own links for further reading.

Just as I was finishing this up, I noticed that Keith of Bit-101 posted his http://www.bit-101.com/blog/I'm sure over the coming weeks we'll see lots of related activity in the blogosphere.

So why even post all this (truth is that most of those links can be found at the bottom of Mike's Delegate article)? Simple really- I'm just trying to figure out how all these methods relate to each other. Obviously some are "replacements" and some are alternatives based on preferences. For example, since Moock's method is an alteration of Mike's EventProxy, but the new Delegate replaces the EventProxy, is Moock's version obsolete? And what about GDispatcher? As far as I can tell, Grant hasn't mentioned it in several months.

Need Professional Help For Your ActionScript Project?
ActionScript.com Consulting Services provide top quality professional ActionScript consulting to businesses around the globe. If you have a professional project in need to world-class talent, tell us about your project by requesting a quote today.

Reader Comments

  1. Todd Coulson  Replied:
    ( 8/4/2004 At 5:58 PM)

    I think Grant Skinner's object was put on hold because of the EULA put out by Macromedia. If you look at all the posts on this site containting "gDispatcher" you will see he has several conversations with Macromedia folks over posting code he created with the dispatchEvent code to help his further development of gDispatcher. Here is a link of his site and the search I did:

  2. Craig Babcock  Replied:
    ( 8/9/2004 At 11:43 PM)

    Robin Drebreuil has developed a "Delegate" style approach that may have a place on your list:

Login to post your comments. If you do not have an account with us please Register.
Copyright 2005 by ActionScript, Inc.   |  Privacy Statement  |  Terms Of Use  |  ActionScript Client Extranet