Trigger event from child to parent in AS3

梦想的初衷 提交于 2020-01-06 08:32:29

问题


I have a MovieClip with a MouseEvent function associated to CLICK event. Inside of this MovieClip I have a TextField.

So, when I click on my MovieClip, the handler works fine. Now I need the same behaviour when I click on this TextField, but without change my handler function. I mean, I don't want to change e.target to e.target.parent if user clicked on TextField instead of MovieClip.

How can I do this?

Some of my source code:

public function Main(){
   var m = new menu();
   menuMng.addChild(m);
   m.addEventListener(MouseEvent.CLICK, openMenu);
}

private function openMenu(e:MouseEvent):void{
   // Do some stuff
}

Thanks in advance!


回答1:


This is a fairly common question - you can use the event.currentTarget property instead of event.target to reference the object to which you added the event listener.

For a more detailed answer check out this question (don't worry about the Flex aspect of it - this relates to standard Actionscript 3 behaviour): What is the difference between target and currenttarget in flex?




回答2:


You can fix problem setting mouseChildren property to false on MovieClip m. In this case click on textfield will trigger MouseEvent like if user click on m. For clarity:

var m = new menu();
m.mouseChildren = false;
...

I hope this will be usefull to you!



来源:https://stackoverflow.com/questions/7953314/trigger-event-from-child-to-parent-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!