How to remove all event listeners at once in AS3

I am making a little game in as3.

The game contains 10 levels.

When I enter level 1, everything is in order. But when I enter the second level (frame), the event listeners from the first frame still work and get a warning saying "Unable to access the object with a null reference to the object." This is because I delete every first level object and add objects from stage 2.

I tried to use removeEventListeners, but this will not work because the ENTER_FRAME listeners work again after removing the Listen Listers.

I tried using different frames for different levels, the bit is not working. I also tried using 1 frmae for all 10 frames, but I get a lot of warnings, and Flash Loader is overloaded.

How can I switch levels (back and forth)? Thanks in advance.

  addEventListener(Event.ENTER_FRAME, subtracting2);
     arrListeners.pop(); // poping it out of the array because it will be deleted after the count reaches 0
     function subtracting2 (e:Event):void
     {
        count--;
        var FAcoef:Number = count/30; //
        FadeAway.alpha = FAcoef; //                   Some effect like FadeAway
        setChildIndex(FadeAway, numChildren - 1); //
        if(count == 0)
       {
            setChildIndex(FadeAway, 0);
            removeEventListener(Event.ENTER_FRAME, subtracting2);
        }
    }
+5
source share
1 answer

There is no built-in way to remove all listeners.

You can use weak references to allow listeners to be deleted when the object is Garbage Collected.

object.addEventListener (......, ......., false, 0, true);

Or you can add the removeAllListeners function yourself, here is some information:
http://blog.reyco1.com/method-of-removing-all-event-listeners/ (look at Ion's comment)

But you will not need any of the above if you immediately want to remove each event listener when it is no longer needed.

, , destroy(). destroy() .

destroy(), ().

+9

All Articles