开发者

as3 stop event propagation

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜