eventListeners Won't Leave

83 views Asked by At

These are the last two lines of action script for my frame :

removeListeners();
if(!stage.hasEventListener(Event.ENTER_FRAME)){trace("STAGE HAS NO eventListeners");}

With the removeListeners() function having been described previously as :

function removeListeners(){
    if(button){
        button.removeEventListener(MouseEvent.CLICK,leaveGameScene);        
    }
    stage.removeEventListener(Event.ENTER_FRAME,menuOnFrame);
    stage.removeEventListener(Event.ENTER_FRAME,collectDrachmas);
    stage.removeEventListener(Event.ENTER_FRAME,updateHealth);
    stage.removeEventListener(Event.ENTER_FRAME,updateCards);
    stage.removeEventListener(Event.ENTER_FRAME,updateQuestions);
    stage.removeEventListener(Event.DEACTIVATE,stageDeactivate);
    stage.removeEventListener(KeyboardEvent.KEY_DOWN,key_down);
    stage.removeEventListener(KeyboardEvent.KEY_UP,key_up);
    stage.removeEventListener(Event.ENTER_FRAME,charEnterFrame);
    stage.removeEventListener(Event.ENTER_FRAME,updateInteractives);
    stage.removeEventListener(Event.ENTER_FRAME,onFrameBadguy);
    stage.removeEventListener(Event.ENTER_FRAME,onFrameStage);
    stage.removeEventListener(Event.ENTER_FRAME,updateConversations);
    stage.removeEventListener(Event.ENTER_FRAME,updatePit);
    //etc etc etc

}

Thus thoroughly removing any possible existing eventListeners that occur throughout my project. We know that no eventListeners are added multiple times, because of the reason given below and because all existing eventListeners are terminated with this code upon exiting a frame.

All eventListeners through my project are simply declared and are not added within other functions or anything like that (ex shown below), therefore removing the possibility of this problem originating from having multiples of the same eventListeners :

stage.addEventListener(Event.ENTER_FRAME,updateHealth);

When the last two lines of the action script for my frame are executed, Flash acts as though there are absolutely no ENTER_FRAME eventListeners on the stage, tracing stubbornly that

"STAGE HAS NO eventListeners"

accordingly. However, after running through the code on this frame multiple ENTER_FRAME eventListeners of the stage are run that had been declared on the previous frame (this causes multiple errors, since currently null objects are being called upon)! The eventListener seems to reappear out of no where and no cause. How can this be possible? What could be the root of this problem?

Perhaps I should mention that I used the exact same method with a button to advance to this frame without trouble. Both ways end with :

removeListeners();
gotoAndPlay(1,"Menu");

Update

Here are the errors given :

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at _109_fla::MainTimeline/collectDrachmas()[_109_fla.MainTimeline::frame1:369]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at _109_fla::MainTimeline/updateHealth()[_109_fla.MainTimeline::frame1:417]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at _109_fla::MainTimeline/updateQuestions()[_109_fla.MainTimeline::frame1:592]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at _109_fla::MainTimeline/updateCards()[_109_fla.MainTimeline::frame1:810]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at _109_fla::MainTimeline/charEnterFrame()[_109_fla.MainTimeline::frame1:978]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at _109_fla::MainTimeline/updateInteractives()[_109_fla.MainTimeline::frame1:1892]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at _109_fla::MainTimeline/onFrameBadguy()[_109_fla.MainTimeline::frame1:2015]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at _109_fla::MainTimeline/onFrameStage()[_109_fla.MainTimeline::frame1:2638]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at _109_fla::MainTimeline/updateConversations()[_109_fla.MainTimeline::frame1:2760]

These errors occur on account of the following eventListeners in their respective functions:

function collectDrachmas(e:Event)
function updateHealth(e:Event)
function updateCards(e:Event)
function charEnterFrame(e:Event)
function updateInteractives(e:Event)
function onFrameBadguy(e:Event)
function onFrameStage(e:Event)
function updateConversations(e:Event)
1

There are 1 answers

1
harilalkm On BEST ANSWER

I couldn't get an idea about your problem. But here is my understanding about it.

  1. stage is a global object. So wherever or whenever you add eventlistener to that object it's listening to the same object. So be care about it.
  2. also using different function for the same event for the same object is a bad idea.
  3. Enterframe is a nasty thing in my experience .
  4. The stage global is accessible to the code only after the display object is added to the stage. That means if you are going to use the stage object inside a class constructer most probable it will throw a null error. Which means we are accessing the object before it available to you. So in you case you may be trying to access the stage object before it's available to you.

Please share some more code so that we could get an idea of whats happening. Then definitely we could help you on this.