AS3 EventManager Class: removeAllListeners

I heard about a class that Grant Skinner wrote called Janitor that was supposed to help keep track of listeners, but I couldn’t find it. Consequently, I wrote my own “EventManager” class for a gaming project I’m working on which keeps track of Event listeners in a project.

As all Actionscript 3 developers know, one of the biggest annoyances is keeping track of listeners and ensuring objects are collected in memory. This class does a lot of that for you, and even has a method removeAllListeners which has different filters. So let’s say some listener keeps calling a function, you can remove all listeners that point to that function. Or let’s say you want to remove ALL key listeners.

Here it is:

*EDIT 9/30/08: EventManager Updated. Click here to get it.

For example, let’s say you have something like so:

var obj:MovieClip = new MovieClip();
var obj2:MovieClip = new MovieClip();
obj = obj2; obj.removeEventListener(Event.ENTER_FRAME,Test); // Does NOTHING!

Test will still be called every frame! Even though weak reference is set to true and there are no more references to obj! This is not good! But with EventManager…

var obj:MovieClip = new MovieClip();
var obj2:MovieClip = new MovieClip();
EventManager.addEventListener(obj,Event.ENTER_FRAME,Test,false,0,true,true); // last parameter actually adds the listener, see documentation in class of why this last parameter exists.
obj = obj2; // Now we have a bunch of options, any of the bottom lines would work EventManager.removeAllListeners(null,Event.ENTER_FRAME); // will remove all Event.ENTER_FRAME listeners
EventManager.removeAllListeners(null,Event.ENTER_FRAME, Test); // will remove all Event.ENTER_FRAME listeners that call Test
EventManager.removeAllListeners(null,null, Test); // will remove all listeners that call Test EventManager.removeAllListeners(); // will remove all listeners

The only problem with the class is that this version does not distinguish useCapture events… But big deal. Maybe I’m naive, but how often does anyone actually set useCapture to true?

Anyway, if this class gets a lot of attention I’ll put it as open source on Google Code. Then someone else can add in functionality for useCapture.

This class was such a pain to code. AS3’s Dictionary is the only sure fire way to index objects as objects (as Array uses the result of toString() for indexes), but because (for some reason that’s beyond me), Adobe decided not to have a length property in Dictionary, my life was made very difficult. There might be some bugs, but based on my initial tests everything seems to be working fine. I wonder how Grant’s Janitor class is compared to mine. I really couldn’t figure out any other way to write the function definitions for EventManager.addEventListener and EventManager.removeEventListener (having actuallyAddListener and actuallyRemoveListener as the last parameter). I wish AS3 had a way to get the memory location value of an object. Something like Object.toMemoryString or something. That way Arrays could be used and the Object.toMemoryString value (which would be unique for every single object) could be used as keys.

If you decide to use this class, please let me know so I know my work hasn’t been in vain!

My Thoughts on Google and Yahoo Indexing Flash Content

Today Google reported that they’ve developed an algorithm to index Flash content (only text, not video or images). This news is clearly dominating Flash news sources around the web, with mostly mixed reviews.

Many bloggers are criticizing Google claiming that it’s impossible for any algorithm to figure out the text information of SWF files which load text from external sources (such as XML) because it’s impossible to know the format of the XML documents being transferred over. But who cares? I still don’t get why people are saying that that’s how the algorithm works. Google has stated that it’s able to crawl externally loaded SWFs (although they don’t couple it with the original SWF when indexing, which is a significant problem for sites that load multiple SWFs for navigation); consequently, they must be monitoring HTTP requests made by the SWF and can do the same with XML files. Google doesn’t need to know how the XML file is parsed… the Flash document will do that for them. They can just have the Flash load the XML file and monitor the text fields and see the value. That’s probably why they say, “To protect your material from being crawled, convert your text to images when possible”.

The only problem I see is if text fields are very dynamic. Maybe the algorithm only goes through static text fields? Because I see no way how a text field that displays random letters (for visual effect) being able to be indexed by any algorithm.

Here’s my prediction: Community tagging. Just like the Google Image Labler game, Google will ask users to tag/label Flash documents that their parser can’t index correctly. Humans would be the perfect computational tool to solve this kind of problem. Yes, there are millions of SWFs out there needing to be indexed, but we really just want the major ones parsed.