You must be logged in to post Login Register
Search 

Next step for Vertical Menu with Actionscript 3 and XML

User Post

8:20 pm
July 30, 2009


Joe

Member

posts 4

1

I'd like to say thanks for the great tutorials.


I've been looking at trying to make this menu include sub menus so in the menu.xml file I've changed the Tutorials button to include and cut out all entries after About:


I've cut the menu code back to the first building blocks. If I click the menu it will output the name of the button clicked. What I would like to do is be able to click the Tutorials button and expand the menu out. Before I get ahead of myself I'd like to see if you click on a button could you output if the button has submenus or not?


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;

function createMenu():void {

    for each (var button:XML in xmlMenu.buttons.button) {

        var menuItem:MenuItem = new MenuItem();

        menuItem.x = 0;
        menuItem.y = count * menuItem.height * 1.05;

        menuItem.menuText.text = button.label.toString();

        menuItem.mouseChildren = false;

        menuHolder.addChild(menuItem);
        menuHolder.addEventListener(MouseEvent.CLICK, callMenu);

        count++;
    }
   
}
function callMenu(e:Event):void{

    var item:MenuItem = e.target as MenuItem;
trace(item.menuText.text);

}
init()


8:59 pm
July 30, 2009


Joe

Member

posts 4

2

Add the following code to the createMenu() function will add the subbuttons to the list. Would this be the best way to do it and then hide/contract them before displaying the list?

if(button.subbutton.length()>1){
           
            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.text = subbutton.label.toString();
       
                subItem.mouseChildren = false;
       
                menuHolder.addChild(subItem);
               
            }
        }

7:50 am
August 1, 2009


Joe

Member

posts 4

3

I don't know if I'm going about this the right way but this is what I have so far. This will expand the menu and add the subbuttons to the menu. I haven't worked out the contracting part but I would say doing the reverse of the expanding should do the trick. I don't think my code is elegant but it is working so far. If anyone has any pointer please share.


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;
       
        if(button.subbutton.length()>0){
            subNum += 1;
            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;
    if(item.value.length > 0){
        for (var i:Number = Number(item.name)+1; i <= count; i++) {
            //trace(”Count is “+i+” nLevel “+nLevel);
            menuHolder.getChildByName(i.toString()).y = nLevel * menuHolder.getChildByName(i.toString()).height * 1.05;
        nLevel++
        }
   
        nLevel = Number(item.name)
        for (var s:Number = 1; s <= item.value.length; s++) {
            menuHolder.getChildByName(item.name+”.”+s.toString()).y = nLevel * menuHolder.getChildByName(item.name+”.”+s.toString()).height * 1.05;
            menuHolder.getChildByName(item.name+”.”+s.toString()).alpha = 1;
            nLevel++
        }
        //trace(”There are submenus”);
       
    }
   
   
}
init()

5:16 am
August 2, 2009


Joe

Member

posts 4

4

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()


Topic RSS 
Search