user3650870
user3650870

Reputation: 17

Flash AS3. Problems with removing EventListener

So I have a bit of a problem here. I will use fake variable names in the codes I'll show to make it simpler. But, I made a function that is called once by certain conditions that are not important to specify. This function creates a variable that is a Function itself. Then, I call a timer eventListener for this function variable to call it in a 'loop'. It works for that part. But then, I want to remove this event listener, but it does not recognize the variable of the function ( that I sent as an argument ) in this timer function. It shows null and it mean I cannot remove the eventListener and the function keeps being called forever. Any solutions?

P.S. myTimer is a public variable available everywhere and I get a 1010 error.

public function calledOnce(){
    this.myFunction = loopFunction( x, y, "function" );
    this.myTimer.addEventListener( TimerEvent.TIMER, this.myFunction );
}
public function loopFunction( refX, refY, refFunction ){
    return function( e: Event ){
        //Do something
        switch( refFunction ){
            case "function":
                this.myTimer.removeEventListener( TimerEvent.TIMER, this.myFunction );
                break;
        }
    };
}

Upvotes: 1

Views: 65

Answers (2)

Vesper
Vesper

Reputation: 18747

You need to return a ready-made function code, without any reference to those refX, refY, refFunction as they are unavailable within the generated function.

public function loopFunction( refX, refY, refFunction ){
    switch( refFunction ){
        case "function":
            return function( e: Event ){
                //Do something
                myTimer.removeEventListener( TimerEvent.TIMER, myFunction );
            }
        }
    }
}

Be aware though that removing a dynamically generated listener isn't easy, to do that, you first have to store the function somewhere (you already have there code to store it in a variable myFunction).

Upvotes: 0

TreeTree
TreeTree

Reputation: 3230

I believe it has something to do with this. If you trace (this); you would get [object YourClass] but when you do that inside the function like so:

public function loopFunction( refX, refY, refFunction ){
    return function( e: Event ){
        trace (this);
        //Do something
        switch( refFunction ){
            case "function":
                myTimer.removeEventListener( TimerEvent.TIMER, myFunction );
                break;
        }
    };
}

You get [global] instead and I have a feeling your class object is no longer there in that scope. I managed to get it running by getting rid of the this and declaring the variables explicitly:

private var myFunction:Function;
private var myTimer:Timer = new Timer (1000);

public function calledOnce(){
    myFunction = loopFunction( x, y, "function" );
    myTimer.addEventListener( TimerEvent.TIMER, this.myFunction );
}
public function loopFunction( refX, refY, refFunction ){
    return function( e: Event ){
        //Do something
        switch( refFunction ){
            case "function":
                myTimer.removeEventListener( TimerEvent.TIMER, myFunction );
                break;
        }
    };
}

Upvotes: 2

Related Questions