AS3:查找您单击的子对象

时间:2011-01-10 00:14:37

标签: flash actionscript-3

我有一个菜单MovieClip,里面有按钮。我有一个带有MousEvent.CLICK的菜单,并试图找到一种方法来注册你点击的内容。希望我对此有效...谢谢!

private function menu_CLICK(e:MouseEvent):void
    {
        //this is where I need help on
        switch (????)
        {
            case "books" :
                showSection("books")
                break;
            case "music" :
                showSection("music")
                break;
            default :

        }
    }

    private function showSection(section:String)
    {
        switch (section)
        {
            case "books" :
                trace("books");
                break;
            case "music" :
                trace("music");
                break;
            default :

        }
    }

2 个答案:

答案 0 :(得分:2)

将事件侦听器添加到每个Button对象中,而不是仅添加一个菜单MovieClip。

您可以使用e.target获取您点击的对象实例。然后,您必须与成员进行比较,例如:if (e.target == books),如果您的菜单按钮名为 books ,或者对于阶段名称:if (e.target.name == "books"),或针对任何自定义属性你添加到按钮。

答案 1 :(得分:0)

如何为每个嵌套剪辑提供ID?

var sections:Array = ['books', 'music', 'other1', 'other2', 'other3']

private function assignClips(){
    // lets say you have 5 buttons/clips inside of your holder movieclip, each named clip0, clip1, etc
    for (var i=0; i<sections.length; i++){
        var mc = holder.getChildByName('clip'+i)
        mc.id = i
        mc.addEventListener.MouseEvent.CLICK, menu_CLICK, false, 0, true)
    }
}

private function menu_CLICK(e:MouseEvent):void
    var id = e.currentTarget.id
    var val = sections[id]
    showSection(val)
}   

private function showSection(section:String){
    trace(section)
}
相关问题