- Home
- Tutorials
- Flash
- Intermediate
- Dynamic menu with submenus
Dynamic menu with submenus

Page 1 of 1
| The root timeline contains a movie clip, called "menu MC", which contains the movie clips needed to grow the menu. In the actions frame of root, a series of variables, representing the menu and submenu items, are declared. These could equally be stored in an external file and loaded into the movie when it is launched. The variable "total" is used to keep track of the number of main menu items. In this example, the submenu items are numbered in such a way that the first number(s) after "stext" represents the number of the menu item calling it, while the last number is the identifier within the menu. For example, stext11 is the first submenu item of main menu item 1, while stext42 is the second submenu item of main menu item 4. You can play around with this numbering naming scheme and find one that suits you. Inside the "menu MC" movie clip are two further clips representing the main menu and submenu buttons. The first step in generating the menu is to create the buttons of the main menu using duplicatemovieclip in a for loop limited by _root.total. |
for (i=1; i<|=_root.total; ++i) {
//duplicate movie clip
menuMC.duplicatemovieclip("menuMC"+i, i);
//then position them one under the other
this["menuMC"+i]._y = this["menuMC"+(i-1)]._y+this["menuMC"+(i-1)]._height;
//The next step is to assign a variable to
//each main menu button, so that when it is clicked,
//there is a simple method for identifying the menu item.
this["menuMC"+i].choice = i;
//Finally, track how many levels have been used to
//create the main menu, so that these can be
//added to as the submenus are duplicated.
levelTrack = i;
}
for (i=1; i<|=_root.total; ++i) {
//check to see if the item has a submenu
if (_root["subText"+i]>=1) {
name = "stext"+i;
for (n=1; n<|=_root["subText"+i]; ++n) {
levelTrack += 1;
subMenuMC.duplicatemovieclip(name+n, levelTrack);
this[name+n]._visible = false;
this[name+n].choice = (i*10) + n;
}
//position first submenu item under parent
this[name+1]._y = this["menuMC"+i]._y+this["menuMC"+i]._height;
//position rest of submenu items
for (n=2; n<|=_root["subText"+i]; ++n) {
this[name+n]._y = this[name+(n-1)]._y+this[name+(n-1)]._height;
}
}
}
function resetMenu() {
for (i=1; i<|=_root.total; ++i) {
if (_root["subText"+i]>=1) {
name = "stext"+i;
for (n=1; n<|=_root["subText"+i]; ++n) {
this[name+n]._visible = false;
}
}
this["menuMC"+i]._y = this["menuMC"+(i-1)]._y+this["menuMC"+(i-1)]._height;
}
}
function submenuShow() {
resetMenu();
//then test to see if the button that has been clicked has a submenu
if (_root["subText"+choice]>=1) {
//if there is a submenu to be displayed then make these menu items visible
name = "stext"+choice;
for (n=1; n<|=_root["subText"+choice]; ++n) {
this[name+n]._visible = true;
}
//and position the rest of the main menu item below the submenu
this["menuMC"+(choice+1)]._y = this[name+(n-1)]._y+this[name+(n-1)]._height;
for (i=choice+2; i<|=_root.total; ++i) {
this["menuMC"+i]._y = this["menuMC"+(i-1)]._y+this["menuMC"+(i-1)]._height;
}
}
}
To use the buttons to load urls, simply add in the appropriate url references within the variable list. For example, to link to nbc.com from the submenu button stext21
stext21 = "puppies";
//this is a reference for a URL that will be launched by the button "puppies"
//any URL should have the variable name urls plus the number of the text name for that button
urls21 = "http://www.nbc.com";
then on the submenu movie clip button add the code:
on (release) {
myURL = eval("_root.urls"+choice);
if(myURL != null){
getURL(myURL);
}
}
To ensure that the url opens in a new browser window, add "_blank" to the getURL call
getURL(myURL, "_blank");
Equally, this can be used to call a URL into a named frame, or to call a JavaScript.
Using a similar approach allows the buttons to access different scenes in the _root, though it is important, when doing so, to remember that scene names will not be recognized from within the nested movie clips of the menu, and that the linkage will have to be to unique frame names.
Have fun!
Spread The Word
Related Articles
2 Responses to "Dynamic menu with submenus" 
|
said this on 22 Jun 2007 12:06:51 PM CDT
Wheres the source file?
|
|
said this on 26 Nov 2007 2:01:25 PM CDT
jason i'm having trouble with this same ideal but with scenes and my button doest work, here my code please help me out this.entire_menu.portfolio_menu.box1.onRelease = function(){
gotoAndPlay("calgary",1); trace(this+" has been presssed"); } |



Author/Admin)