Post edited 5:19 am – August 2, 2009 by Joe
Post edited 5:19 am – August 2, 2009 by Joe
This is and isn't working. If I only have one menu with subbuttons the menu works fine. If I add subbuttons to another menu item they all seem to appear under the first menu item that has subbuttons. The issue must be with the way I'm creating the array. If anyone has any suggestions it would be greatly appreciated.
var strXMLPath:String = “menu.xml”;
var xmlLoader:URLLoader;
var xmlMenu:XML;
function init():void {
xmlLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, onXMLLoadComplete);
xmlLoader.load(new URLRequest(strXMLPath));
}
function onXMLLoadComplete(e:Event):void {
xmlMenu = new XML(e.target.data);
createMenu();
}
//Create a menu holder that will contain all the menu items
var menuHolder:MovieClip = new MovieClip();
menuHolder.x = myMask.x;
menuHolder.y = myMask.y;
menuHolder.mask = myMask;
addChild(menuHolder);
var count:Number = 0;
var menuNum:Number = 1;
var subNum:Number = 0;
function createMenu():void {
for each (var button:XML in xmlMenu.buttons.button) {
//trace(button.subbutton.length());
var menuItem:MenuItem = new MenuItem();
var listSubMenus:Array = new Array();
menuItem.x = 0;
menuItem.y = count * menuItem.height * 1.05;
menuItem.menuText.text = button.label.toString();
menuItem.name = menuNum.toString();
menuItem.mouseChildren = false;
//This is the part I'm having an issue with
if(button.subbutton.length()>0){
subNum += 1;
listSubMenus.push(”Close”);
for each (var subbutton:XML in xmlMenu.buttons.button.subbutton) {
//count++;
var subItem:MenuItem = new MenuItem();
subItem.x = 0;
subItem.y = count * subItem.height * 1.05;
subItem.menuText.x = 10;
subItem.menuText.text = subbutton.label.toString();
subItem.name = menuNum +”.”+ subNum;
subItem.value = “”;
listSubMenus.push(subItem.name);
subItem.mouseChildren = false;
subItem.alpha = 0;
menuHolder.addChild(subItem);
menuHolder.setChildIndex(subItem, 0);
subNum += 1;
}
}
menuItem.value = listSubMenus;
menuHolder.addChild(menuItem);
menuHolder.addEventListener(MouseEvent.CLICK, callMenu);
count++;
menuNum++
}
}
function callMenu(e:Event):void{
var item:MenuItem = e.target as MenuItem;
//trace(”Text “+item.menuText.text);
//trace(”Name “+item.name);
//trace(”Y “+item.y);
//trace(”Value “+item.value);
var nLevel:Number = Number(item.name) + item.value.length-1;
if(item.value.length > 0){
if(item.value[0] == “Close”){
item.value[0] = “Open”;
for (var md:Number = Number(item.name)+1; md <= count; md++) {
//trace(”Count is “+i+” nLevel “+nLevel);
menuHolder.getChildByName(md.toString()).y = nLevel * menuHolder.getChildByName(md.toString()).height * 1.05;
nLevel++
}
nLevel = Number(item.name);
for (var so:Number = 1; so < item.value.length; so++) {
menuHolder.getChildByName(item.name+”.”+so.toString()).y = nLevel * menuHolder.getChildByName(item.name+”.”+so.toString()).height * 1.05;
menuHolder.getChildByName(item.name+”.”+so.toString()).alpha = 1;
nLevel++
}
}else{
item.value[0] = “Close”;
for (var sc:Number = 1; sc < item.value.length; sc++) {
menuHolder.getChildByName(item.name+”.”+sc.toString()).y = item.y;
menuHolder.getChildByName(item.name+”.”+sc.toString()).alpha = 0;
}
nLevel = Number(item.name);
for (var mu:Number = Number(item.name)+1; mu <= count; mu++) {
//trace(”Count is “+i+” nLevel “+nLevel);
menuHolder.getChildByName(mu.toString()).y = nLevel * menuHolder.getChildByName(mu.toString()).height * 1.05;
nLevel++
}
}
}
}
init()