2011-03-13 59 views
0

我有一個Flex應用程序裏子調度父母內部的事件:如何以更加動作友好的方式傾聽事件?

孩子:

<fx:Metadata> 
[Event(name="myEvent", type="flash.events.Event")] 
</fx:Metadata> 

<fx:Script> 
<![CDATA[ 
dispatchEvent(new Event("myEvent")); 
]]> 
</fx:Script> 

父:

<comp:poppy id="myPopup" myEvent="myEventStuff(event);" /> 

我已經改變了我的代碼,以便創建myChild在actionscript中,而不是如圖所示的mxml標籤。如何在actionscript中添加事件偵聽器?你可以這樣做

public function showPoppy(evt:MouseEvent):void { 

var myPopup:poppy = PopUpManager.createPopUp(this, poppy, true) as poppy; 

PopUpManager.centerPopUp(myPopup:poppy); 
myPopup:poppy.includeInLayout = true; 
myPopup:poppy.visible = true; 

// how do I add the event listener? 

} 

回答

0

一種方法是罌粟類中在你的罌粟類突圍「myEvent」字符串靜態字符串:

public static const MY_EVENT:String = "myEvent"; 

然後,你的ActionScript組件可以偵聽此事件從您的「myPopup」實例:

myPopup.addEventListener(poppy.MY_EVENT, handleMyEvent); 
0

而只是一個細節,你不應該放一些裸露的代碼。喜歡這樣的事情:

<s:Group ... creationComplete="onCreationComplete"> 
<fx:Metadata> 
[Event(name="myEvent", type="flash.events.Event")] 
</fx:Metadata> 

<fx:Script> 
<![CDATA[ 
private function onCreationComplete(e:Event):void 
{ 
    dispatchEvent(new Event("myEvent")); 
} 
]]> 
</fx:Script>