Search code examples
actionscript-3flashevent-handlingmouseeventaddchild

actionscript 3.0 function mouseevent event handler


I have a function that uses a mouse event and it removes and adds things onto the stage:

beginBut.addEventListener(MouseEvent.CLICK, bgnListener);
function bgnListener (event:MouseEvent) {
    removeEventListener(Event.ENTER_FRAME, setScreen);
    removeChild(beginBut);
    removeChild(myWord);

    healthBar.addEventListener(Event.ENTER_FRAME, healthLose);
    ball.addEventListener(Event.ENTER_FRAME, moveBall);
    myGem.addEventListener(Event.ENTER_FRAME, addGem);
    myScore.addEventListener(Event.ENTER_FRAME, scoreCount);
    healthBar.width+=1000;

}

However after some other things happen, I need this event to occur again. I have already added beginBut but when I use

beginBut.addEventListener(MouseEvent.CLICK, bgnListener);

the event adds and removes the things automatically when the function that adds beginBut back occurs and not when I actually click on beginBut. I have also tried

bgnListener();

but it says that there is the wrong number of arguments. I already searched all over and I can't seem to fix this. Any help would be greatly appreciated.


Solution

  • If you call bgnListener() like you are now, you'll get an argument mismatch error because the function is expecting to receive a MouseEvent.

    If you want to be able to call bgnListener() on its own like that, you can define a default value for your argument event, which can be null:

    function bgnListener(event:MouseEvent = null)
    {
        // ...
    }