Actionscript 3 AS3:将按钮行为仅放在容器的子mcs上的最佳方法是什么

Actionscript 3 AS3:将按钮行为仅放在容器的子mcs上的最佳方法是什么,actionscript-3,flash,button,event-bubbling,event-propagation,Actionscript 3,Flash,Button,Event Bubbling,Event Propagation,我有一个容器,里面有很多子容器。我希望子菜单具有完整的按钮式行为(单击可启用,光标效果) 我不想让每个孩子都有单独的鼠标侦听器。 ... 我只想在父容器上有一个侦听器,尽管父容器实际上是不活动的。。。只有子类才能定义类中按钮的常见行为,并将该类与库中的按钮符号相关联。您可以在容器上收听单击事件,并将使用捕获标志设置为true(addEventListener的第三个参数)-每次单击任何容器的子对象(包括子对象等)时都会调用侦听器。然后,您可以通过检查按钮的名称属性来检查单击了哪个按钮。假设您有如

我有一个
容器,里面有很多子容器。我希望子菜单具有完整的按钮式行为(单击可启用,光标效果)

我不想让每个孩子都有单独的鼠标侦听器。
... 我只想在父容器上有一个侦听器,尽管父容器实际上是不活动的。。。只有子类才能定义类中按钮的常见行为,并将该类与库中的按钮符号相关联。

您可以在容器上收听
单击
事件,并将
使用捕获
标志设置为
true
addEventListener
的第三个参数)-每次单击任何容器的子对象(包括子对象等)时都会调用侦听器。然后,您可以通过检查按钮的
名称
属性来检查单击了哪个按钮。

假设您有如下内容:

var container_mc:Sprite = new Sprite();
addChild(container_mc);

container_mc.addChild(button1);
container_mc.addChild(button2);
//and so forth with all your buttons

要执行您的要求,您可以执行以下操作:

//add a click listener to the container
container_mc.addEventListener(MouseEvent.CLICK,click);

//make the buttonMode true on the container, so you get the button hand cursor
container_mc.buttonMode = true;

function click(e:Event):void {
    //e.target is a reference to what was clicked (see caveat after code sample)

    //if all the children of container_mc are of the same custom class, 
       //you could now call a click handler on that item

    MyButtonClass(e.target).myClickHandler(e);

    //or you could use a switch statement
    switch(e.target){
        case button1:
            //button 1 was clicked, do something with it
            break;

        case button2:
            //button 2 was clicked, do something with it
            break;
    }
}
button1.mouseChildren = false;
这里唯一需要注意的是,事件的目标可能是容器下面的任何子对象。因此,如果
button1
有一些子对象,并且这些子对象有子对象,则
e.target
中引用的对象可以是其中的任何一个(单击过)。如果按钮中有子对象,则确保目标始终是按钮的最简单方法是执行以下操作:

//add a click listener to the container
container_mc.addEventListener(MouseEvent.CLICK,click);

//make the buttonMode true on the container, so you get the button hand cursor
container_mc.buttonMode = true;

function click(e:Event):void {
    //e.target is a reference to what was clicked (see caveat after code sample)

    //if all the children of container_mc are of the same custom class, 
       //you could now call a click handler on that item

    MyButtonClass(e.target).myClickHandler(e);

    //or you could use a switch statement
    switch(e.target){
        case button1:
            //button 1 was clicked, do something with it
            break;

        case button2:
            //button 2 was clicked, do something with it
            break;
    }
}
button1.mouseChildren = false;
这将确保按钮发送的任何鼠标事件都将
button1
作为目标,而不是它的任何子项

请忽略否决票。。。如果你的问题和我的一样, 那么这个解决方案就行了


但我相信我需要在mc类中放置一个eventlistener。。。因此,mc的每个实例都会有效地创建一个独特的侦听器实例……您不在每个按钮上放置侦听器的动机是什么?按钮的光标效果是属于该按钮的功能。此外,没有“侦听器实例”这样的东西。事件系统只是要调用的函数引用的列表。如果侦听器在mc类中,则mc的每个实例都将注册一个唯一的侦听器。。。但这是一个很好的策略,否则……这毫无意义,而且是一个非常糟糕的答案。它不会解释任何东西,也不会对未来的访问者有帮助。我想说的是,只有代码的答案通常是不受欢迎的。最重要的是,您发布了伪代码,而伪代码的后半部分没有多大意义(至少对我来说),也不会解决您的问题——尽管这可能只是一个输入错误。无论如何,这是一个糟糕的答案(不管它是否对你“有效”)。好的答案解释代码,格式良好,解决了这个问题,而不是仅仅发布一些没有解释的伪代码。至于“它只需要容器上的一个事件侦听器”,显然我知道这一点,正如您将看到的,我在5天前回答了这个问题,并向您解释了很多。