views:

41

answers:

1

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 dont want it to.

this.addEventListener(MouseEvent.CLICK, firstlistener);

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

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.stopImidiatePropagation();
    }
}

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.

Sam