AS3 removing an event handler within a function - actionscript-3

I am adding an eventListener within the first function and wish to get rid of it in the second. Removing the eventListener doesn't seem to be working and as such each subsequent call to the first function is adding more event handlers. I know one way to get around this is to not add the eventListener within the function, but doing it this way will save me a lot of work for the rest of the code. Any ideas?
function errorBoxHandler(event:Event):void
{
this.errorBox.errorOkBtn.addEventListener(
MouseEvent.MOUSE_DOWN,
function(event:MouseEvent)
{
errorBoxOkHandler(event, btnSelected, listIndexNum)
}
);
}
function errorBoxOkHandler(event:MouseEvent, btnSelected:String, listIndexNum:int):void
{
this.errorBox.errorOkBtn.removeEventListener(MouseEvent.MOUSE_DOWN, errorBoxOkHandler);
}

It happens because you are adding an anonymous function as a listener
this.errorBox.errorOkBtn.addEventListener(MouseEvent.MOUSE_DOWN,
function(event:MouseEvent){//here
errorBoxOkHandler(event, btnSelected,listIndexNum)
});
Adding errorBoxOkHandler directly as a listener like this:
this.errorBox.errorOkBtn.addEventListener(MouseEvent.MOUSE_DOWN, errorBoxOkHandler);
will make it removable with the
this.errorBox.errorOkBtn.removeEventListener(MouseEvent.MOUSE_DOWN,errorBoxOkHandler);
call
You will have to find a way to make btnSelected and listIndexNum visible from errorBoxOkHandler because as event listener it can accept only one argument

Related

in AS3, removeEventListener(Event.ENTER_FRAME) is not working

I have been dealing with this problem for days already. I am at my wits' end!
I can't seem to find a definitive answer anywhere on any of the forums, documentation, etc.
Everything looks fine at first run, or when I load a next level for the user to play. But if the user hits the ESC key to load a different level, the ENTER FRAME listener does not get removed and it duplicates all the triggers in it, showing the player going really fast, and all funky, because it builds on top of the previously instantiated ENTER FRAME listener.
I don't know if I have a problem of an anonymous function, or an unknown instance being referenced in my removeEvent... command... Bottom line, I give up and I need this working HELP!!!
Here's the code:
function initPlay():void
{
//code here determining what display object to add to the list and assign it to the currentLevel variable (a movieclip)
if(userIsLoadingOtherLevel){
removeEnterFrameListener();
addChild(currentLevel);
}
if(userIsGointToNextLevel)
addChild(currentLevel);
currentLevel.addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
//collision detection, parallax scrolling, etc, etc is done here.
if(allCoinsCollected)
loadNextLevel();
if(ESCKeyPressed)
ESCKeyPressHandler();
}
function loadNextLevel():void
{
removeChild(currentLevel);
newLevelToLoad++
removeEnterFrameListener();
initPlay();
}
function ESCKeyPressHandler():void
{
removeChild(currentLevel);
initPlay();
}
function removeEnterFrameListener();
{
currentLevel.removeEventListener(Event.ENTER_FRAME,onEnterFrame)
trace("currentLevel.hasEventListener(Event.ENTER_FRAME) = "+currentLevel.hasEventListener(Event.ENTER_FRAME)); //outputs TRUE if called from loadNextLevel but FALSE if called from initPlay() !!!
}
}
I also tried to add and remove the eventListener to stage, MovieClip(Root), or nothing at all and the result is always the same.
I know that there may be other ways to design such a process, but please note I am not really flexible at the moment on doing this because the project is very long (about 4000 lines of code) and removing the ENTER FRAME this way, crazy or not should still work!!
THANK YOU in advance for anyone willing to help.
The problem appears to be the nested functions inside the initPlay() method.
Each time you call initPlay() you are defining new functions. Some of these nested functions call initPlay() themselves.
Functions are objects (memory references). So each time you call initPlay() you are making new references to new functions. So when you try to remove an event listener, you're only able to remove one of these event handlers (the one in the current scope of execution).
I'm not sure if I'm explaining this clearly, perhaps this example will help. I'll use numbers to represent the references to each function, and a simple scenario that is similar to yours:
function example():void
{
addEventListener(MouseEvent.CLICK, mouseClickHandler);
function mouseClickHandler(event:Event):void
{
if (someCondition)
{
example();
}
else
{
removeEventListener(MouseEvent.CLICK, mouseClickHandler);
}
}
}
When we run this function the first time, a new function is defined within the scope of the example() function. Lets use the number 1 to represent the reference to this nested function. someCondition is true on the first time around, and so the example() function is called again.
On the second execution of the example() function, a new reference to the mouse event handler is created (#2). We also add the event listener again. At this point, there are two event handling functions in memory, and both will be executed when the event is dispatched.
Let's say that in the second invocation of example() that someCondition is false and now we want to remove the listener. When we call:
removeEventListener(MouseEvent.CLICK, mouseClickHandler);
It's referring to event handler #2. Event handler #1 still exists, and because it's hidden in the scope of the first invocation of example() it can't be removed here.
My simple example breaks down after this... but I hope it makes it clear why your event handlers shouldn't be nested inside a function. Admittedly, this is difficult to describe and even more so in a real world example like yours. But I'm pretty confident that this is the source of most, if not all, of the issues you describe.
Here's how I was able to get around this without changing the scope of the nested functions (although I agree that would be the preferred solution) by creating a boolean variable called "loadingNewGame" and changing it to true from outside the onEnterFrame (in fact, this assignment was done from initPlay() and then from onEnterframe I called removeEnterFrameListener() function. This did the trick.
here's the code in case anybody is interested:
// package, and other code here.
var loadingNewGame:Boolean = new Boolean(false);
function initPlay():void
{
//code here determining what display object to add to the list and assign
//it to the currentLevel variable (a movieclip)
if(userIsLoadingOtherLevel)
{
loadingNewGame = true;
removeEnterFrameListener();
addChild(currentLevel);
}
if(userIsGointToNextLevel)
addChild(currentLevel);
loadingNewGame:Boolean = false;
currentLevel.addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
if(loadingNewGame)
removeChild(currentLevel);
//collision detection, parallax scrolling, etc, etc is done here.
if(allCoinsCollected)
loadNextLevel();
if(ESCKeyPressed)
ESCKeyPressHandler();
}
function loadNextLevel():void
{
removeChild(currentLevel);
newLevelToLoad++
removeEnterFrameListener();
initPlay();
}
function ESCKeyPressHandler():void
{
initPlay();
}
function removeEnterFrameListener();
{
currentLevel.removeEventListener(Event.ENTER_FRAME,onEnterFrame)
trace("currentLevel.hasEventListener(Event.ENTER_FRAME) = "+currentLevel.hasEventListener(Event.ENTER_FRAME));
//outputs true
}

Calling certain functions whitout having the right arguments

I have two function on my AS3 program, one fires when the width and height changes:
stage.addEventListener(Event.RESIZE, resizeListener);
function resizeListener (e:Event):void {
//some commands
}
And the second one fires one a number of milliseconds pass:
var myTimer:Timer = new Timer(clockUpdate, 0);
myTimer.addEventListener(TimerEvent.TIMER, updateData);
myTimer.start();
function updateData(e:TimerEvent):void {
trace("AUTOUPDATE");
trace(e);
}
I need to fires those function also manually, lets say when the user press a button, but i don't know what parameters i have to send them when they are called manually.
I tried just resizeListener() and updateData() but of course it fails asking me for the parameter.
You can make parameters in a function optional by providing a default value. This is an example by taking your two functions above and making the event parameters optional:
function resizeListener(e:Event = null):void {
//some commands
}
and
function updateData(e:TimerEvent = null):void {
trace("AUTOUPDATE");
trace(e);
}
Calling, for example, resizeListener() will now execute the function and the value of e will default to null.
Making the Event parameter optional, resizeListener(e:Event=null), as in walkietokyo's answer, is a perfectly valid and often convenient solution. Another alternative is to put the stuff you want to be able to do without the event being triggered in a separate function, that can be called by the event handler and from anywhere else.
So assuming for example that what you want to do on resize is to rearrange the layout, and you also want to do that same layout setup at initialization, or at the click of a button, or anytime really, you could do something like this:
stage.addEventListener(Event.RESIZE, resizeListener);
function resizeListener(e:Event):void {
rearrangeLayout();
}
function rearrangeLayout():void {
// The actual rearrangement goes here, instead of in resizeListener. This can be called from anywhere.
}
Which way to do it is probably a matter of taste or can vary from case to case, really, both works fine.
A benefit of separating things in an event handler and another function is that there will not arise a situation where you would have to check if the e:Event parameter is null or not. In other words, you would have code that is dependent on the Event, if any, in the event handler, and code that is independent of the Event in a more general function (not an event handler).
So in a more general and schematic case, the structure would be something like this:
addEventListener(Event.SOME_EVENT, eventListener);
function eventListener(e:Event):void {
// Code that needs the Event parameter goes here (if any).
// Call other function(s), for the stuff that needs to be done when the event happens.
otherFunction();
}
function otherFunction():void {
// Stuff that is not dependent on the Event object goes here, an can be called from anywhere.
}

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.
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)
{
// ...
}

Removing Event Listener in Deactivate/Activate Event - AS3

I am using the code snippet from Flash Cs6 for my mobile Air app. How do I properly remove the listener? ( I get error 1120: Access of undefined property ocean_slider. ) with the following code. Thanks for your help.
/* Deactivate/Activate Event
Conserve CPU and battery life by suspending expensive processes, such as ENTER_FRAME and TIMER events, when the application is not in focus.
Instructions:
1. Start timers and add event listeners in "fl_Activate".
2. Stop timers and remove event listeners in "fl_Deactivate".
*/
stage.addEventListener(Event.ACTIVATE, fl_Activate);
stage.addEventListener(Event.DEACTIVATE, fl_Deactivate);
function fl_Activate(event:Event):void
{
// Start timers and add event listeners here.
naturepage.sliders.ocean_slider.addEventListener(Event.ENTER_FRAME, ocean_slider);
function ocean_slider(e:Event):void
{
ocean_transform.volume = (naturepage.sliders.ocean_slider.value/100);
ocean_channel.soundTransform = ocean_transform;
}
}
function fl_Deactivate(event:Event):void
{
// Stop timers and remove event listeners here.
naturepage.sliders.ocean_slider.removeEventListener(Event.ENTER_FRAME, ocean_slider);
}
OK. I just changed the code to the following but still have the problem. The listener is definitely getting added because the functions work in my app after publishing without the removeEventListener. But once I add the code to remove the listener, I get the error 1120 when I try to publish.
stage.addEventListener(Event.ACTIVATE, fl_Activate);
stage.addEventListener(Event.DEACTIVATE, fl_Deactivate);
function fl_Activate(event:Event):void
{
addEventListener(Event.ENTER_FRAME,myFunction);
function myFunction(event:Event):void
{
ocean_transform.volume = (naturepage.sliders.ocean_slider.value/100);
ocean_channel.soundTransform = ocean_transform;
}
}
function fl_Deactivate(event:Event):void
{
removeEventListener(Event.ENTER_FRAME,myFunction);
}
It looks like your brackets are off - is that the code you copied and pasted or what you're trying to run. I'll edit the code after you confirm why the brackets are the way they are - specifically the 2 '}' in the ocean_slider function
Regardless, it means that ocean_slider isn't available and hasn't been added to the stage. So if it has, you need to check to make sure you're not calling fl_Deactivate before things are ready. And you also have a function call and stage item with the same name - ocean_slider - I'd change that and see if it works.
ok. I think it's working now. I put the function all the way outside. Thanks for your help and responses.
function myFunction(event:Event):void
{
ocean_transform.volume = (naturepage.sliders.ocean_slider.value/100);
ocean_channel.soundTransform = ocean_transform;
}
stage.addEventListener(Event.ACTIVATE, fl_Activate);
stage.addEventListener(Event.DEACTIVATE, fl_Deactivate);
function fl_Activate(event:Event):void
{
addEventListener(Event.ENTER_FRAME,myFunction);
}
function fl_Deactivate(event:Event):void
{
removeEventListener(Event.ENTER_FRAME,myFunction);
}

I need help bubbling an event in AS3

I want to have the parent of my class handle the event first, then I want to have the child handle the event. Is there a way to explicitly bubble the event up? I want to do something like this:
...
this.addEventListener(MouseEvent.CLICK, characterClicked);
...
private function characterClicked(e:Event):void{
// pass event to parent to be handled first
...
}
Is this possible, and if so how?
There are three "phases" of an event; Capture, At target and Bubble. They occur in this order, meaning that if you set an event listener to be in the capture phase it will always fire before one set regularly (which would mean either at target or bubble).
Like so:
// in parent, third argument is "use capture"
child.addEventListener(MouseEvent.CLICK, handleClickInParent, true);
// in child, add listener as usual
addEventListener(MouseEvent.CLICK, handleClick);
Now, your parent event listener will always fire first!
I figured out a way to do this. Is seems hackish, but it works. If there is a better way of doing this please let me know.
...
this.addEventListener(MouseEvent.CLICK, characterClicked);
...
private function characterClicked(e:Event):void{
// pass event to parent to be handled first
this.removeEventListener(MouseEvent.CLICK, characterClicked); //prevent infinite loop
dispatchEvent(e); // send event to parent object
this.addEventListener(MouseEvent.CLICK, characterClicked);
e.stopImmediatePropagation();
...
}
If you were to handle the listener in the parent instead of the child it might be easier. Then you could just pass the event to the child when you're done:
// inside parent class:
childObj.addEventListener(MouseEvent.CLICK, onCharacterClicked);
private function onCharacterClicked(e:Event):void {
// do parent stuff first
// ...
// then pass event to child
childObj.onCharacterClicked(e);
}