2014-02-09 36 views
0

我有一些問題,抓住了聽者自定義事件。ActionScript 3.0中聽了一出動自定義事件

我有許多稱爲鍵的對象。每個鍵在被點擊時調度一個自定義事件,如下所示:

public class Key extends Sprite 
{ 
    private var letter:String; 
    public static const CLICKED:String = "clicked"; 

    private function keyClicked(e:MouseEvent):void { 
     this.removeEventListener(MouseEvent.CLICK, keyClicked, false); 
     this.mouseEnabled = false; 
     dispatchEvent(new Event(CLICKED)); 
    } 
} 

所有鍵都是鍵盤對象的子項。一個鍵盤對象的父母有一個事件監聽器,如下:

addEventListener(Key.CLICKED, keyboardGuess); 

這就要求

public function keyboardGuess(e:Event):void { 
     trace("event received"); 
     var letter:String = e.target.getLetter(); 
     trace(letter); 
    } //there will be other functionality in here when I get it listening 

然而,儘管我可以告訴大家,我成功地調度的情況下,我的聽衆是從來沒有采摘它起來。我已經爲此瘋狂了一個多小時;你能幫我弄清楚發生了什麼事嗎?

回答

1

自定義事件的氣泡值應該是真實的,所以包含密鑰可以收到該事件的對象。如果氣泡值爲false,則只有派發事件的對象可以接收事件,例如Key。

關於bubbles

嘗試

dispatchEvent(new Event(CLICKED, true)); 
+0

dispatchEvent不會默認氣泡爲真? –

+0

不可以,因爲我正在調度一個默認爲false的新事件,這就是爲什麼我在API中找不到它;我正在查看dispatchEvent,而不是事件構造函數。這正好解決了我的問題。非常感謝! –

相關問題