Flion
Flion

Reputation: 10902

adding an EventListener while such an event is being triggered without having it triggered too

When the escape key is released, I close the deepest child in a tree-like structure, and then tell it's parent (which is now the new 'deepest child') to close when the escape key is released ... but I do that while such an event is triggered, that causes the whole chain to close.

Any tips on how to overcome this problem?

Upvotes: 0

Views: 121

Answers (3)

PatrickS
PatrickS

Reputation: 9572

Not sure how you go about it but in the example below, I make sure to remove the event listener on the deepest child before adding it to the next one.

Of course you can add the stopImmediatePropagation() method, as mentioned in the other answers to ensure the event doesn't bubble up to other objects. I'm not sure that event bubbling would affect the remaining children in this example, but if it does, that would definitely be the answer to the problem and you could call that method in the eventHandler function , anywhere before calling initEvent().

private function initEvent(child:Sprite):void
{
    child.addEventListener( KeyboardEvent.KEY_UP , eventHandler );
}

private function eventHandler(event:KeyboardEvent):void
{
   if( event.charCode == //whatever the charCode is for the escape key )
   {
       var deepestChild:Sprite = event.currentTarget;
       deepestChild.removeEventListener( KeyboardEvent.KEY_UP , eventHandler );

       var parent:Sprite = deepestChild.parent;
       parent.removeChild(deepestChild );

       if( parent != null )
           initEvent( parent )

   }
}

Upvotes: 0

Jonatan Hedborg
Jonatan Hedborg

Reputation: 4432

See if calling stopPropagation() on your Event instance does the trick.

Upvotes: 0

Timofei Davydik
Timofei Davydik

Reputation: 7294

Try add event.stopImmediatePropagation() at the end of the listener.

Upvotes: 1

Related Questions