AS3:我可以将事件从一个孩子分发到另一个孩子吗? [英] AS3: Can I dispatch Event from one Child to another?

查看:71
本文介绍了AS3:我可以将事件从一个孩子分发到另一个孩子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我无法对这个谜进行梳理:)。

Hi guys, I cannot sort out this enigma :).

如何从Child1发送dispatchEvent并在Child2中捕获此事件?

How to send dispatchEvent from Child1 and catch this event in Child2?

如何从Child1发送dispatchEvent并在Child3中捕获该事件?

How to send dispatchEvent from Child1 and catch this event in Child3?

如何从Child3发送dispatchEvent并在Child1中捕获该事件?

How to send dispatchEvent from Child3 and catch this event in Child1?

谢谢大家。我会在这里为您提供帮助!

Thanks guys. I will appreciate some help here!

推荐答案

尽管我得到了您想要实现的目标,但我认为更好的设计方法是允许容器来管理它的孩子。

Although I get what you are trying to achieve, I think the better design approach is to allow the container to manage it's children.

例如,如果容器收到child1的事件,则容器的职责是在适当时通知其他孩子。这种方法还使容器代码中的所有子容器之间的基本交互非常清楚。如果我要查看某人的代码,出于这个原因,我将不胜感激。

For example if container receives an event from child1, it's container's role to take care of notifying the other children if appropriate. This approach also makes it very clear in the container code the basic interaction between all the container's children. If I were going over someone's code, I'd appreciate such methodology for that reason.

话虽如此,您可以在child2和child3中使用一个方法,该方法可以让您传递child1作为参数,然后在其中添加child1的侦听器。例如:

That being said, you could have a method in child2 and child3 that would allow you to pass child1 as a parameter, and then add a listener for child1 within. For example :

在容器代码中:

child2.setChildListener(child1);
child3.setChildListener(child1);

然后在child2和child3中,您可以执行以下操作:

Then in child2 and child3 , you could do something like this :

public function setChildListener(childToListenTo:MovieClip):void
{
    childToListenTo.addEventListener(MouseEvent.CLICK, childClickedHandler);
}

private function childClickedHandler(e:MouseEvent):void
{
    // react to the child click here
}

这篇关于AS3:我可以将事件从一个孩子分发到另一个孩子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆