2011-10-10 60 views
0

當釋放按鍵時,我會以樹狀結構關閉最深的孩子,然後告訴其父母(現在是新的「最深的孩子')在退出鍵被釋放時關閉......但是,當這樣的事件被觸發時,我會這樣做,導致整個鏈條關閉。添加EventListener,同時觸發這樣的事件,但不會觸發它

有關如何解決此問題的任何提示?

+0

檢查'Event.bubbles'(http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html#bubbles)爲TRUE; – www0z0k

回答

1

嘗試在偵聽器的末尾添加event.stopImmediatePropagation()

0

看看你的事件實例調用stopPropagation()是否有用。

0

不知道你如何去做,但在下面的例子中,我確保在將最深的孩子添加到下一個孩子之前刪除事件監聽器。

當然,您可以添加stopImmediatePropagation()方法,如其他答案中所述,以確保事件不會冒泡到其他對象。我不確定在這個例子中事件冒泡會影響剩餘的子元素,但是如果是這樣的話,那肯定會是問題的答案,您可以在調用initEvent()之前的任何位置在eventHandler函數中調用該方法。

 
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) 

    } 
}