开发者

as3 stop event propagation

开发者 https://www.devze.com 2023-01-08 06:42 出处:网络
I\'m having trouble getting events to stop propagating in as3 when I\'m adding a new event listener in an event listener.My second event listener is being called when I don\'t want it to.

I'm having trouble getting events to stop propagating in as3 when I'm adding a new event listener in an event listener. My second event listener is being called when I don't want it to.

this.addEventListener(MouseEvent.CLICK, firstlistener);

f开发者_StackOverflowunction firstlistener(e:Event)
{
    //do stuff
    e.stopImmediatePropagation();
    this.addEventListener(MouseEvent.CLICK, secondlistener);
}


You can add both firstListener and secondListener at the same time, but set the priority for the first to be higher. That way it can conditionally stop propagation to the second.

this.addEventListener(MouseEvent.CLICK, firstlistener, false, 100);
this.addEventListener(MouseEvent.CLICK, secondlistener);

function firstlistener(e:Event)
{
    if (...condition...) {
        e.stopImmediatePropagation();
    }
}

but if you have control over both listeners, then it might be better to conditionally call the second from the first or broadcast a second, different. A little cleaner than using stopImmediatePropagation.


Since this is a MouseEvent, try passing:

e:MouseEvent

as the handler's argument and not:

e:Event
0

精彩评论

暂无评论...
验证码 换一张
取 消