If you’ve ever tried to use fl.removeEventListener(), you’ve probably run into the following situation.
Part the First, in which you check the documentation.
It reads:
Usage
fl.removeEventListener(eventType)Parameters
- eventType
- A string that specifies the event type to remove from this callback function. Acceptable values are “documentNew”, “documentOpened”, “documentClosed”, “mouseMove”, “documentChanged”, “layerChanged”, and “frameChanged”.
Returns
A Boolean value of true if the event listener was successfully removed; false if the function was never added to the list with the fl.addEventListener() method.
Description
Unregisters a function that was registered using
fl.addEventListener().Example
The following example removes the event listener associated with the documentClosed event:
fl.removeEventListener("documentClosed");
CS4 documentation can be found online here, CS5 here
Part the Second, in which You Reasonably Assume that the Code Following Would Work
fl.addEventListener("documentChanged", onDocChange);
function onDocChange() {
fl.trace("Document changed.");
}
// elsewhere in your script...
fl.removeEventListener("documentChanged");
Part the Third, in which You Run the Code Preceding and Concordantly Receive the Error Following
The following JavaScript error(s) occurred:
At line 1 of file “/Users/dru/Desktop/tmp.jsfl”:
Wrong number of arguments passed to function “removeEventListener”.
Part the Fourth, in which You Try to Reason With the Documentation
So, then you think, “the documentation is wrong, and we need to pass the listener function in as an extra argument, that must be what this error is about.” So you try the same thing, only with this line:
fl.removeEventListener("documentChanged", onDocChange);
Which makes more sense anyway, right? But…
Part the Fifth, in which you Run the Code Preceding and Yet are Still Rewarded With Errors
The following JavaScript error(s) occurred:
At line 1 of file “/Users/dru/Desktop/tmp.jsfl”:
removeEventListener: Argument number 2 is invalid.
This seems to verify that indeed there are two parameters for fl.removeEventListener, but the second one is not the listener function.
Part the Sixth, in which You Give Up
You figure it can’t be the end of the world if you never release your event listeners…
Good news!
After filing a bug with Adobe on this, I got a response stating that the API has changed with Flash CS4, but the documentation was never updated. Adobe is, according to the email I received, working on updating the docs, but until they do, here’s the scoop.
fl.addEventListener is also mis-documented. It actually returns a numeric ID, identifying that particular event listener connection. The mysterious second parameter to fl.removeEventListener is that numeric ID. Not unlike the old setInterval/clearInterval workflow from pre-AS3 days.
So, this works:
var changeEventId = fl.addEventListener("documentChanged", onDocChange);
function onDocChange() {
fl.trace("Document changed.");
}
// elsewhere in your script...
fl.removeEventListener("documentChanged", changeEventId);
Ta-da!
Personal aside
Honestly, this is a bit disappointing on Adobe’s part. It’s been mis-documented for two major releases of Flash Professional, not to mention that they just willy-nilly changed the API. Event listening code that works in CS3 does not work in CS4 or CS5, and vice-versa. On top of that, I feel like the numeric ID aspect is just a bit dumb. It’s supposed to be so that it can be utilized from anywhere, as long as you have the right numerical ID, and that makes sense that one script may not have a reference to the actual listener function from another script. But still. Maybe a both-techniques-work approach would be useful here. Let us pick the one that’s most convenient.
Conclusion, in which You Now Know How to Properly Use fl.removeEventListener()
You’re welcome.

5 comments
Comments feed for this article
December 4, 2010 at 12:55 am
Keldon Rush
Thank you indeed – this was maddening and I could not fathom what was happening. When you add a listener to “documentOpened” you had better remove it or every time a FLA is opened *boom* goes the script.
Thanks very much for figuring this out and sharing it.
June 19, 2011 at 3:58 pm
daveystew
Dru, brilliant. I was about to give up as well!
November 9, 2011 at 7:04 pm
Mike S
I must extend a “thank you.”
Thank you!!!
November 24, 2011 at 4:55 pm
Dru Kepple
Mike, getting your comment about a year after this post was written made me wonder if that documentation ever got updated. I checked (links are in the post body), and nope, they are not. At least in the CS5 docs, a user named “steamboy” has left comments that indicate the actual API.
Come on, Adobe; I know JSFL is something of a disowned bastard child, but it surely can’t take that long to properly update two pages of your documentation.
(Not that I’m one to talk, what with our months of silence on the blog…)
February 15, 2012 at 5:48 pm
luke
Thanks a lot! I couldn’t figure out how to stop traces happening every time i changed document, this explains it perfectly!