Copy link to clipboard
Copied
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);
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Well I am new to this that's for sure, so I appreciate the help. Let's say I have a gameshow that asks multiple choice questions with 4 answers. I need to know which answer they click on, run a function that I need to pass the correct answer to, increment the correct/incorrect runners, and pass what the next questions is so I send them to the next question. Utilizing the object code you gave me I was able to pass everything except which answer they clicked on because it seems you can't pass variables through event listeners. So how else would you know which answer they clicked on without creating a unique handler for each one?
Copy link to clipboard
Copied
You can, if you make your own events:
Find more inspiration, events, and resources on the new Adobe Community
Explore Now