# Horizontal XML Menu - vary item width help

**URL:** https://forum.kirupa.com/t/horizontal-xml-menu-vary-item-width-help/67266
**Category:** flash
**Created:** [October 14, 2004, 3:55am UTC](https://forum.kirupa.com/t/horizontal-xml-menu-vary-item-width-help/67266 "2004-10-14T03:55:31Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![plonker](https://avatars.discourse-cdn.com/v4/letter/p/439d5e/32.png) [@plonker](https://forum.kirupa.com/u/plonker)
#### Post date: [October 14, 2004, 3:55am UTC](https://forum.kirupa.com/t/horizontal-xml-menu-vary-item-width-help/67266/1 "2004-10-14T03:55:31Z")

</div>

Howdy everybody.

I was workin away on this file and wanted to display the main menu items horizontally using the XML menu. I got it to work but wanted to take it a step further and display the main menu items according to a variable width based on the character length from the xml file. I managed to get an idea (I think) of how this might be done but I think i need a little help finishing it up. Here is the AS for the menu:  
[AS]  
// generates a list of menu items (effectively one menu)  
// given the inputted parameters. This makes the main menu  
// as well as any of the submenus  
GenerateMenu = function(container, name, x, y, depth, node\_xml) {  
// variable declarations

var curr\_node;  
var curr\_item;  
var curr\_menu = container.createEmptyMovieClip(name, depth);  
// for all items or XML nodes (items and menus)  
// within this node\_xml passed for this menu  
for (var i=0; i\<node\_xml.childNodes.length; i++) {  
// movieclip for each menu item  
curr\_item = curr\_menu.attachMovie(“menuitem”,“item”+i+"\_mc", i);

if (name == “mainmenu\_mc”) {  
//curr\_item.name.\_width = i+(curr\_item.name.text.length+50);  
curr\_item.\_x = x + i\*(curr\_item.\_width+1);  
curr\_item.\_y = y;  
} else {  
curr\_item.\_x = x;

}  
curr\_item.trackAsMenu = true;

// item properties assigned from XML  
curr\_node = node\_xml.childNodes\*;  
curr\_item.action = curr\_node.attributes.action;  
curr\_item.variables = curr\_node.attributes.variables;  
curr\_item.name.text = curr\_node.attributes.name;

// item submenu behavior for rollover event  
if (node\_xml.childNodes\*.nodeName == “menu”){  
// open a submenu  
curr\_item.node\_xml = curr\_node;  
curr\_item.onRollOver = curr\_item.onDragOver = function(){  
var x = this.\_x ;  
var y = this.\_y + this.\_height+2;  
GenerateMenu(curr\_menu, “submenu\_mc”, x, y, 1110, this.node\_xml);

};  
}else{ // nodeName == “item”  
//curr\_item.arrow.\_visible = false;  
// close existing submenu  
curr\_item.onRollOver = curr\_item.onDragOver = function(){  
curr\_menu.submenu\_mc.removeMovieClip();

};  
}

curr\_item.onRollOut = curr\_item.onDragOut = function(){

};

// any item, menu opening or not can have actions  
curr\_item.onRelease = function(){  
Actionsthis.action;

CloseSubmenus();

};  
} // end for loop  
};  
// create the main menu, this will be constantly visible  
CreateMainMenu = function(x, y, depth, menu\_xml){  
// generate a menu list  
GenerateMenu(this, “mainmenu\_mc”, x, y, depth, menu\_xml.firstChild);

// close only submenus if visible durring a mouseup  
// this main menu (mainmenu\_mc) will remain  
mainmenu\_mc.onMouseUp = function(){  
if (mainmenu\_mc.submenu\_mc && !mainmenu\_mc.hitTest(\_root.\_xmouse, \_root.\_ymouse, true)){  
CloseSubmenus();  
}  
};  
};

// closes all submenus by removing the submenu\_mc  
// in the main menu (if it exists)  
CloseSubmenus = function(){  
mainmenu\_mc.submenu\_mc.removeMovieClip();  
};  
// This actions object handles methods for actions  
// defined by the XML called when a menu item is pressed  
Actions = Object();  
Actions.loadExternal = function(externalswf){  
container.loadMovie(externalswf);  
};  
Actions.gotoURL = function(urlVar){  
getURL(urlVar, “\_blank”);  
};  
Actions.message = function(msg){  
message\_txt.text = msg;  
};  
Actions.newMenu = function(menuxml){  
menu\_xml.load(menuxml);

};  
// load XML, when done, run CreateMainMenu to interpret it  
menu\_xml = new XML();  
menu\_xml.ignoreWhite = true;  
menu\_xml.onLoad = function(ok){  
// create main menu after successful loading of XML  
if (ok){  
CreateMainMenu(88, 353, 0, this);  
unloadMovie(“container”);  
}else{  
message\_txt.text = “error: XML not successfully loaded”;  
}  
};  
// load first XML menu  
menu\_xml.load(“menu1.xml”);  
[/AS]

The notable part here maybe this commented piece which is my “idea” as to how to get this to work (see commented line)  
[AS]  
if (name == “mainmenu\_mc”) {  
//curr\_item.name.\_width = i+(curr\_item.name.text.length+50);  
curr\_item.\_x = x + i\*(curr\_item.\_width+1);  
curr\_item.\_y = y;  
} else {  
curr\_item.\_x = x;

}  
[/AS]

If anyone needs anymore explanation as to what I am trying to achieve just let me know.

Thanks for taking up this challenge.
