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 overco开发者_Go百科me this problem?
Try add event.stopImmediatePropagation()
at the end of the listener.
See if calling stopPropagation()
on your Event instance does the trick.
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 ) } }
精彩评论