Dynamic Functions for a menu in Flash

Posted on 2004-11-24
Last Modified: 2010-04-03
Hi all...

I have a Flash movie that reads in a varying number of items from an array.  I want it to dynamically create the onRelease functions for the item.

For Example:

Menu 1 has 3 items, so Flash creates MENU 1, with 1 2 3 underneath.  when you click on 1, you bring up item1, click on 2, bring up item2, etc.
Menu 2 has 5 items, flash does as above, but with 5 clickable links going to item1-5

Now, i have it hardcoded as "btn1.onRelease = function() { blah }" "btn2.onRelease..." etc. but i wanted to know if there's a dynamic way to do it.

Any help will be appreciated. An answer to the above or a better way to do it will be accepted. Thanks!
Question by:carlenevs
    LVL 9

    Accepted Solution

    The following code i took from an XML menu I created (did it based on an XML-menu tutorial):

            subs = menu.firstChild.childNodes.childNodes;
            amount = subs.length;
            for (b=0; b<=amount; b++) {
                _root.attachMovie("submenuItem", "subitem"+b, b+20);
                _root["subitem"+b].nametxt = subs[b];
                _root["subitem"+b].itemUrl = subs[b].attributes.theURL;
                _root["subitem"+b].itemTarget = subs[b].attributes.Target;

                // put them in the right possition
                _root["subitem"+b]._y = topypos+(_root["subitem"+b]._height*b)+(_root["item"+nummer]._height);
                _root["subitem"+b]._x = startxpos;

    In the above there is a button-movie ready for duplication. So the button holds the "onClick" action allready.

    In your situation it would be something like this:
    for (b=0; b<subbuttonamount; b++) {
                 this["btn" + b].onRelease = function() { getURL("abc.htm") };

    Hope the examples get you further!?
    LVL 9

    Expert Comment

    recommend points for dutchfoxer  :)

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
    There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
    The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
    The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now