Skip to main content
Participant
November 25, 2014
Question

Yet another removeEventListner not working?

  • November 25, 2014
  • 1 reply
  • 472 views

So I have an event listener that I add to a movie clip.  The movie clip always exists on the root timeline, and all this code is also on the root timeline.  When the user click's the movie clip I want to remove the listener so I can add a new one a few frames later.  However, no matter what I do I can't get the eventListener to remove.  I've even tried to add it and remove it directly after adding it, but no matter what I do I can't get it to remove...and I can still click the movie clip, and the function gets called. So a few frames later when I add the listener again and click the movie clip again, the function get's called twice instead of just once.  I can't figure out what I'm missing.  Any insight would be great.  Here is my current code with the removeEventListener right after the add:

var tmp:String = "1st";

var question:String = "What is the question?";

answerAmc.answerAanimation.answerA.text = "To the A";

var regis:MovieClip = regis_1;

var nextquestion:String = "q2";

var mycorrect:String = "C";

var overallCorrect:int = 0;

var overallIncorrect:int = 0;

answerAmc.addEventListener(MouseEvent.CLICK, onObjectClickedHandlerA(mycorrect, "A", nextquestion, regis, tmp));

answerAmc.removeEventListener(MouseEvent.CLICK, onObjectClickedHandlerA(mycorrect, "A", nextquestion, regis, tmp));

      

function onObjectClickedHandlerA(thiscorrect:String, thisanswer:String, thisnextquestion:String, thisregis:MovieClip, temp:String):Function {

  //this is the nested event handler to bring my variables into scope

  return function(e:MouseEvent):void {

         if (thiscorrect == thisanswer) {

              //Play some movie clips and increment our "correct' tracking variable, then move to the next question.

             Correct.gotoAndPlay(2);

              Correct.gotoAndStop(2);

              overallCorrect = ++overallCorrect;

              trace(overallCorrect);

              thisregis.gotoAndStop(1);

              SoundMixer.stopAll();

              gotoAndPlay(thisnextquestion);

           

           

          } else {

               //Play some movie clips and increment our "incorrect' tracking variable, then move to the next question.

              Correct.gotoAndPlay(23);

              Incorrect.gotoAndStop(2);

              overallIncorrect = ++overallIncorrect;

               trace(overallIncorrect);

             thisregis.gotoAndStop(1);

              SoundMixer.stopAll();

              gotoAndPlay(thisnextquestion);

          }

          trace (temp);

         // correct.text = color;

         //trace(color);

         // trace(thiscorrect);

         // trace(thisanswer);

       

       

      }

}

This topic has been closed for replies.

1 reply

Inspiring
November 26, 2014

That is some nasty nested code.

If you absolutely must have complex args triggered on a button click try something like this:

answerAmc.addEventListener(MouseEvent.CLICK, clickHandler);

//create your own property

answerAmc.params = new Object(mycorrect, "A", nextquestion, regis, tmp);

function clickHandler(e:MouseEvent):{

//get to the params of the MovieClip

var currentParams:Object = e.currentTarget.params;

//get to every part of the object with currentParams[0],currentParams[1] etc and use that to set up your logic

}

//remove the eventListner when you don`t need it anymore with

//answerAmc.removeEventListener(MouseEvent.CLICK, clickHandler);

The even better solution would be to use a Custom Event.

http://snipplr.com/view/41351/

noim988Author
Participant
November 26, 2014

Awesome, that works, and is so much cleaner!  I just then had to create a click handler for each answer that is available for them to click on (so I know which one they are clicking on), instead of using the same handler for each one. 

Inspiring
November 27, 2014

Then you are doing still sth. wrong. I don´t know the details of your setup but as soon as you start numbering your handlers you are not seeing the bigger picture. But...whatever works for you.