[noob] Help understanding why event listener continues even after removal...
Hey once again,
I'll try to do my best to explain. I can get this to work correctly, but I'd like to understand why what I was doing earlier wasn't working.
I'm having a little trouble understanding why an event listener continues listening even after I remove it. I have a set-up where, a Parent MovieClip ("CampScene") is established, and an event listener is added to listen for a function ("walkUpToCampfire") to see if a condition is true ("comingFromAx == true", which won't be true until later).
If it is true, an animation plays, an event listener is added to see if the animation is done, and if it is an event listener is added to listen for a click ("campScene.goToAx") for a new animation to play and other events to occur. (It won't be set to true until later.)
If it isn't true (which is the situation I'm working with initially), the event listener told to listen for a click on "campScene.goToAx" is set up immediately. The function that follows removes the event listener, thus it's suppose to stop listening for a click and let an animation play all the way through.
The problem is a click continues to be listened for, and if one keeps clicking the animation restarts each time, which is what I don't want to happen.
--------------------------------------
I'm able to solve the problem by adding the line "campScene.removeEventListener(Event.ENTER_FRAME, walkUpToCampfire);" or by setting mouseEnabled to false for "campScene.goToAx", but I'm trying to understand what is going on.
Is it, even though I'm removing the ("campScene.goToAx") listener in one function ("goToAxScene"), "campScene.addEventListener(Event.ENTER_FRAME, walkUpToCampfire);" continues to listen for a mouse click unless it is removed also?
Again, I'm just trying to understand how this is working.
--------------------------------------
This should be all the relevant AS. Naturally, let me know if more information is needed. (I can post the FLA/AS files if need be.)
public function practice_adventure8() {
...
//initial event listeners
addEventListener(Event.ENTER_FRAME, CampScene);
}
public function CampScene(event:Event) {
//remove the event listener
removeEventListener(Event.ENTER_FRAME, CampScene);
//add the initial camp scene
campScene.x = -120;
campScene.y = -10;
addChild(campScene);
setChildIndex(campScene, 0);
//add event listener to see if guy walking to campfire or not
campScene.addEventListener(Event.ENTER_FRAME, walkUpToCampfire);
}
//IF WALKING BACK TO CAMPSCENE
public function walkUpToCampfire(event:Event) {
//if guy is walking back from ax
if (comingFromAx == true) {
//event listeners
campScene.guyAtCampScene.gotoAndPlay("guyComingFromAx");
campScene.addEventListener(Event.ENTER_FRAME, ifGuyDoneWalking);
//else if initial creation
} else {
//remove old event listener
//campScene.removeEventListener(Event.ENTER_FRAME, walkUpToCampfire);
//trace("comingFromAx=FALSE");
//event listeners
campScene.addEventListener(Event.ENTER_FRAME, sceneHoverInfo);
campScene.goToAx.addEventListener(MouseEvent.MOUSE_UP, goToAxScene);
//campScene.goToBridge.addEventListener(MouseEvent.MOUSE_UP, goToBridgeScene);
}
}
//IF WALKING BACK TO CAMPSCENE DONE
public function ifGuyDoneWalking(event:Event) {
//if guy has finished walking
if (campScene.guyAtCampScene.currentLabel == "guyAtCampSceneNormal") {
//reset the scene booleans
comingFromAx = false;
//add the event listeners
campScene.addEventListener(Event.ENTER_FRAME, sceneHoverInfo);
campScene.goToAx.addEventListener(MouseEvent.MOUSE_UP, goToAxScene);
}
}
//GET READY FOR NEW SCENE
public function goToAxScene(event:MouseEvent) {
//remove any movement event listeners
campScene.goToAx.removeEventListener(MouseEvent.MOUSE_UP, goToAxScene);
//stop all relevant sounds
SoundMixer.stopAll();
//play clip of guy walking to AxScene
campScene.guyAtCampScene.gotoAndPlay("guyGoingToAx");
//check for label to see if axScene should appear
campScene.guyAtCampScene.addEventListener(Event.ENTER_FRAME, addScene);
}
