简体   繁体   中英

remove ENTER_FRAME EventListener from inside this as3

This is my code in Flash/AS3, in main class.

addEventListener(Event.ENTER_FRAME,function(e:Event){

        if(findObject == true){
            // I want to remove this ENTER FRAME
        }

});

尝试这个:

e.currentTarget.removeEventListener(e.type, arguments.callee)
  1. You shouldn't be doing what you do in the code above.

  2. The mgraph's code has a tiny chance of failing to work as advertised if the currentTarget of the event doesn't have a removeEventListener() method (possible, but very unlikely). From the compiler standpoint though you will be trying to dynamically resolve the method on a generic object which is error prone and should be handled with care. This is hazardous because it shows that the programmer "did not know" what kind of object was she expecting to handle and worked by assumption. Assumptions are great for finding a solution but are equally bad for implementing one.

If you thought of optimizing something in the way you did it, then, just FYI this actually creates a unique (redundant) name in the symbol table (in the compiled SWF file) which causes worse compression of the SWF.

If you are doing this as a matter of experiment, this is fine, but you should avoid such code in real life projects.

One more thing to be aware of: comparison to true constant is 100% useless. If such comparison makes any sense at all (ie findObject may evaluate to false any time), then if (findObject) { ... } is equivalent but shorter version of your code. Last thing, hopefully, the anonymous function is missing return type declaration. It won't really change much in your example, except that you will get compiler warning. Omitting type declaration is, in general, a bad style.

EDIT

public function addEventListener(type:String, listener:Function ...):void
{
    this._listeners[type].push(listener);
}

public function dispatchEvent(event:Event):void
{
    for each (var listener:Function in this._listeners[event.type])
        listener(event);
}

public function removeEventListener(type:String, listener:Function, ...):void
{
    delete this._listeners[type][listener];
}

Suppose you actually want to implement IEventDispatcher (instead of using another EventDispatcher - you may have your reasons to do so, one such reason is that native EventDispatcher generates insane amounts of short-lived objects - events, and you may want to reduce that.) But there is no way you can replicate event.target or event.currentTurget in your code because you can't access the object owning the method, so, you would leave that out.

Another example:

public class SomeEvent extends Event
{

    private var _target:NotEventDispatcher;

    public function SomeEvent(type:String, someTarget:NotEventDispatcher)
    {
        super(type);
        this._target = someTarget;
    }

    public override function get target():Object
    {
        return this._target;
    }
}

This is something that I actually saw in real world, this was used in either Mate or similar framework to sort of "anonymously" connect all event dispatchers to a single static instance of some "mothership event dispatcher".

I don't necessarily justify this approach, but, technically, nothing stops you from doing either one of these. What I was saying in my post above is that in certain situations the language promises you things, like, if you did:

var dispatcher:IEventDispatcher;
try
{
    dispatcher = IEventDispatcher(event.currentTarget);
    // now you can be sure this object has removeEventListener
    dispatcher.removeEventListener(event.type, arguments.callee);
}
catch (error:Error)
{
    // but what are you going to do here?
}

But the most common case would be you subscribing to a bubbling event, in which case, you don't know whether you want to unsubscribe from event.target or event.currentTtarget - because you don't know which one is that you are listening to.

I agree with wvxvw.

Another way to approach your problem is to have a variable to control the "state" of your ENTER_FRAME event:

private var _state:String;

private function init(e:Event):void {
    addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}

private function loop(e:Event):void {
    switch(_state) {
        case "play":
            // do play stuff
            // when you want to pause
            // goToPause();
        break;
    }
}

// you can call the method below from a button or whatever you want
private function goToPause():void {
    _state = "pause";
    // do some stuff here
    // when you are done, switch "_state" back to "play"
}

In this example, you keep listening for ENTER_FRAME, but it only does things when the _state variable is set to "play". You can also remove the event listener in the goToPause method:

private function goToPause():void {
    _state = "pause";
    removeEventListener(Event.ENTER_FRAME, loop);
}

However, the nice thing about using the "_state" to switch things is that you don't end up having a mess of addEventListeners and removeEventListeners (which is what can happen depending on how complicated your loop gets) that you have to keep track of.

You should not use anonymous function call if you would like to remove listener some time later.

public function main():void
{
    //...
    //some method, where you add event listener
    //...

    //adding enterFrame event listener
    this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);

    //...

}

private function enterFrameHandler(e:Event)
{

    if(findObject)  // " == true" is not really necessary here.
    {
        // removing enterFrame listener:
        this.removeEventlistener(Event.ENTER_FRAME,enterFrameHandler);
    }
}

Just for a completeness with the other techniques mentioned here, the function you are creating is a unbound closure, so you can also leverage that concept to reference both your function and dispatcher.

var callback:Function;
var dispacher:IEventDispatcher = this;
addEventListener(Event.ENTER_FRAME, callback = function(e:Event){

        if(findObject == true){
            dispacher.removeEventListener(Event.ENTER_FRAME, callback);
        }

});

Normal closed-over variable rules apply.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM