jhultgre
jhultgre

Reputation: 122

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);

function firstlistener(e:Event)
{
    //do stuff
    e.stopImmediatePropagation();
    this.addEventListener(MouseEvent.CLICK, secondlistener);
}

Upvotes: 2

Views: 5937

Answers (2)

Samuel Neff
Samuel Neff

Reputation: 74909

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.

Upvotes: 5

mrmotta
mrmotta

Reputation: 1

Since this is a MouseEvent, try passing:

e:MouseEvent

as the handler's argument and not:

e:Event

Upvotes: 0

Related Questions