0

I'm almost finished with a quiz of mine. I've encountered an error which I have finally pinpointed. But first I'll tell you some background info on the code.

Background

On stage, there exist 4 button components registered as movie clips. These are later stored in an array. When a certain icon is clicked on stage, these buttons are to be activated by adding event listeners to all of them. When a button is pressed, it checks if it's the 'correct' answer and removes the event listeners.

Now I have done extensive checks and narrowed down the problem to the following.

Code

This function will add button listeners to each button recursively. Note that the variable 'num' is a fixed integer between 1 - 4 which is generated earlier into the code and used for many 'if' cases.

function addButtonListener(num:int):void
{
    for (var obj:Object in _buttons)
    {   
        _buttons[obj].addEventListener(MouseEvent.CLICK, checkans(num));
    }
}

This function is basically the opposite and also disables the buttons

function removeButtonListener(num:int):void
{
    for (var obj:Object in _buttons)
    {
        _buttons[obj].removeEventListener(MouseEvent.CLICK, checkans(num));
        _buttons[obj].enabled = false;
    }
}

Now one thing I noticed through the use of trace functions is that the code correctly adds the button listeners but it does not remove them.

This function calls for each button to be removed.

function checkans(num:int):Function
{
    return function(e:MouseEvent):void
    {
        if (e.currentTarget.label == xmlNodes)
        {
            points = points + (2 * num);
            scoreBox.text = points.toString();
        }
        else
        {
            trace("Incorrect!");        
        }
            if(myText.parent){
            myText.parent.removeChild(myText)
        }
            closeShowQuestion(num);//closes a previous function
            removeButtonListener(num);//Should I pass this variable into the end function?
            GoFishing.addEventListener(MouseEvent.CLICK, fish);//Starts the process again.
        }
    }

So am I removing the event listeners incorrectly?

Do you need to see more code to be sure?

Thanks in advance!

2
  • Why do you use function in a function? Any reason? Mar 18, 2015 at 7:53
  • I used it to pass a variable continuously to all the functions.
    – Keno
    Mar 18, 2015 at 16:12

1 Answer 1

0

removeEventListener must use the SAME function that was used in addEventListener.

Your checkans function always returns a NEW function, so it will not be the same for add and remove.

If you have to pass a num into your listener function, do it other way. Make it a property of a button or store externally etc.

2
  • Ok I'm going to make the variables public and store them from the start.
    – Keno
    Mar 18, 2015 at 16:13
  • Amazing! So I edited a couple functions to use a variable which basically took the purpose of num. A couple reference cleanups later, and I had perfectly working code! Thank you very much!
    – Keno
    Mar 18, 2015 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.