Question

I'm working on a Flash project which is separated into separate scenes.

In Scene 1 I have multiple MovieClips (which include event listeners for RESIZE (and others) inside them).

In Scene 2 I have a few common MovieClips and new ones (which also include event listeners for RESIZE (and others) inside them).

After clicking a button from Scene 1 to go to Scene 2, it's fine, except for if I resize the stage and then I get the following error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

I know it's related to the event listeners, but it would be unrealistic to remove them each individually as it's expected there will be many.

Was it helpful?

Solution

If I undertand your situation correctly, I think you will in the end need to remove each listener individually, or add the resize listener only once. Since you mentioned scenes, am I right to assume you are working on the timeline? I also am assuming the null object reference error comes from a scene that has been removed from the stage, making reference to a display object that is no more, a ref to the stage after the scene has been removed, or just calling a function (the resize handler) on an object that no longer exists.

Some way to deal with this are:

  1. Add some checking in the listener handler functions

    if (!this.stage) return

    To avoid the errors, but will not help if the object the function is a method of has been removed.

  2. To avoid needing remember to remove hundreds of listeners, create removeAllListeners and addCustomEventListener functions. Instead of the usual addEventListener, call you addCustomEventListener which in turn will call addEventListener. Have addCustomListener store the target, listener function and event string in a dictionary or array of objects. removeAllListeners can loop through the dictionary or array and remove all your listeners. It is a bit like setting up an event hub, but does not go quite that far.

  3. Instead of adding the RESIZE event listener to each scene, add it only once. Then in the listener function call a function on whichever scene is the active scene or view. This last one is the approach I have seen most often, and is the most bullet proof. It may be tricky to implement on the time line, I have always been a little hazy on timeline variable scope.

OTHER TIPS

Yes, so far as I know there is no good automated way to do this, however it would be a good practice to create a registerAllListeners and a removeAllListeners methods that manually add and remove the appropriate listeners to your object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top