我如何以更加动作友好的方式倾听活动?

时间:2011-03-13 19:35:01

标签: flex actionscript-3

我有一个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);" />

我已经更改了我的代码,因此我在actionscript中创建了myChild,而不是如图所示的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?

}

2 个答案:

答案 0 :(得分:0)

你可以这样做的一种方法是将poppy类中的“myEvent”字符串分解为poppy类中的静态字符串:

public static const MY_EVENT:String = "myEvent";

然后,您的ActionScript组件可以从“myPopup”实例中侦听此事件:

myPopup.addEventListener(poppy.MY_EVENT, handleMyEvent);

答案 1 :(得分: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>