PDA

View Full Version : cant remove event listener (function in a function)



noverflow
August 29th, 2008, 11:13 AM
I have this function, that when given a movie clip and a destination, it moves it to there, and eases out.

I have buttons that control where it moves to. This all works great.
But I want it so that if it is moving, and a new destination is clicked, it stops and moves to the new destination.

When I remove the event listener, nothing happens. But the event listener removes properly when it stops. (just wont when it is moving)

So the tricky part is this. I have a function in a function, It is there so the moving event:enter_frame parrt can see the same variables passed to it from the click.

If I move this function out of the other function, I can remove it. But it can not access the variables and does not move.

Part that does not work
if (isMoving == true) {// are we moving?
trace('is moving:'+isMoving);
removeEventListener(Event.ENTER_FRAME, mover);// stop moving



function easeTo(toMove:MovieClip, dest:Number):void {

trace(toMove +' is being moved to: '+ dest);

if ( toMove.x != dest ) {//are we already there? If so, do nothing
if (isMoving == true) {// are we moving?
trace('is moving:'+isMoving);
removeEventListener(Event.ENTER_FRAME, mover);// stop moving
} else {// not moving, set dest and go.
trace('is moving:'+isMoving);// reads faluse
isMoving = true;// now set to true

addEventListener(Event.ENTER_FRAME, mover);// start moving
}
} else {
trace('we are already there');// do nothing
}

function mover(event:Event) {// move. Function here because it needs to access internal varibles

if (Math.abs(dest - toMove.x) < 1) { // are we there? yes? stop event listener
removeEventListener(Event.ENTER_FRAME, mover);
isMoving = false;
wDirection = '';
} else {
toMove.x += (dest - toMove.x) * speed;
}

}
}

devonair
August 29th, 2008, 02:33 PM
Why not promote the local variables to class members? Nested functions are always messy..

senocular
August 29th, 2008, 03:09 PM
despite it being a nested function it should have access to its own removal since that functions scope inherits the scope of its creator (where the function reference lives).

But I agree that nested functions are messy and should be avoided.

noverflow
August 30th, 2008, 08:04 AM
I ended up doing this. I just hate having to call a function, but then also set the variables outside so that the event listener can see them as well. (why do it twice)

EG.


function home(event:MouseEvent):void {
easeTo(globalWrapper, 0, event.target);

toMove = globalWrapper;
dest = 0;
allowMouse = event.target;
}


If I could just pass variables with event listener additions, it would be so much cleaner.

devonair
August 30th, 2008, 08:14 AM
I would make a Mover class (that would probably implement an IMover interface) that had a destination and isMoving properties. Your easeTo method would then take only a single argument (toMove:IMover) . The toMove object could listen for the ENTER_FRAME event which could then check the currentTarget property of the event itself to, in turn, check the destination property..