Tom
Tom

Reputation: 1095

AS3: Finding the child object you are clicking on

I have a menu MovieClip that has its buttons inside of it. I have the menu with a MousEvent.CLICK and trying to figure a way to register what you are clicking on. Hopefully I'm being efficient about this... Thanks!

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 :

        }
    }

Upvotes: 0

Views: 3656

Answers (2)

jpea
jpea

Reputation: 3079

How about just giving each nested clip an 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)
}

Upvotes: 0

weltraumpirat
weltraumpirat

Reputation: 22604

Add event listeners to the each of the Button objects, instead of just one to the menu MovieClip.

You can get the object instance you are clicking on using e.target. Then you either have to compare against members, for example: if (e.target == books), if your menu button is named books, or against the stage name: if (e.target.name == "books"), or against any custom property you add to the button.

Upvotes: 2

Related Questions