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.

About these ads