2009-01-31 35 views

回答

1

我可以用更一般的術語來詢問你試圖做什麼嗎?

如果你只是想避免空引用錯誤,只需要檢查你使用的null引用(變量):

if (_myComponent != null) 
{ 
    // add listeners 
} 

如果因爲某些其他原因,你真的需要知道,如果該部件已經在你做這件事之前初始化,你需要它爲此發送一些事件(Flex的UIComponents調度FlexEvent.INITIALIZE事件,但我假設你沒有使用Flex)或者設置一個「初始化」屬性,我不太確定標準的Flash組件(我在這裏查看the Button documentation以供參考)。當然,您可以製作自定義的任何組件的子類自己在哪裏實現initialized屬性,但我不確定如何爲任何標準Flash組件實現此功能。我也不太清楚你在這方面的意思是「初始化」:)

0

也許是這樣的?

if (mcWithComponents.stage) doStuff(); 
mcWithComponents.addEventListener(Event.ADDED_TO_STAGE, doStuff); 

private function doStuff(e:Event = null):void 
{ 
    mcWithComponents.myButton.addEventListener(MouseEvent.CLICK, buttonClicked); 
} 

或者將偵聽器直接添加到動畫片段的類中。

public function mcWithComponents() // constructor 
{ 
    if (stage) init(); 
    addEventListener(Event.ADDED_TO_STAGE, init); 
} 
private function init(e:Event = null):void 
{ 
    myButton.addEventListener(MouseEvent.CLICK, buttonClicked); 
} 
相關問題