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.
- http://www.philterdesign.com/blog/archives/000013.html
- A few days later, http://www.gskinner.com/blog/archives/000027.html
- A couple months later, http://chattyfig.figleaf.com/ezmlm/ezmlm-cgi/1/98729
- That prompted Mike Chambers to release http://www.markme.com/mesh/archives/004286.cfm a month later
- In March, Darron Schall did a http://www.darronschall.com/weblog/archives/000100.cfm
- In Moock's book (http://www.macromedia.com/devnet/mx/flash/articles/comp_moock.html), he alters Mike's EventProxy slightly.
- 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.