Hello, for this xml menu when you click a button the submenu will load, and only the submenu have link to an url.
If we press the main btn (MAIN BTN) will load google-yahoo submenu.
The xml :
<menus>
<menu nume="MAIN BTN">
<subNume link="http://google.com">google</subNume>
<subNume link="http://yahoo.com">yahoo</subNume>
</menu>
</menus>
On fla we have a movie clip with class “MenuItem” and the as:
import caurina.transitions.Tweener;
const ITEM_HEIGHT:Number = -25;
var xmlPath:String = "menu.xml";
var xml:XML;
var igrec:Number = 15;
var selectedMenu:Array;
var menuCounter:uint = 0;
var loader = new URLLoader();
loader.load(new URLRequest(xmlPath));
loader.addEventListener(Event.COMPLETE, xmlLoaded);
function xmlLoaded(e:Event):void {
if ((e.target as URLLoader) != null ) {
xml = new XML(loader.data);
xml.ignoreWhitespace = true;
createMenus();
}
}
function createMenus():void {
for each (var menu:XML in xml.menu) {
var menuItems:Array = createMenu(menu);
for (var i= 0; i< menuItems.length; i++) {
menuItems*.y = igrec;
menuItems*.x = (menuCounter * 131) - 130;
addChild(menuItems*);
}
}
}
function createMenu(menu:XML):Array {
//Create an array which contains all the items in this menu
var menuArray:Array = new Array();
//Loop through the items found in the menu
for each (var subNume:XML in menu.subNume) {
//Create a new menu item
var menuItem:MenuItem = new MenuItem();
//Set the item text
menuItem.menuText.text = subNume.toString();
//Add a link variable (the URL where this item should link to)
menuItem.link = subNume.@link;
//Add a mouse listener for each submenu
menuItem.addEventListener(MouseEvent.CLICK, submenuClicked);
menuItem.addEventListener(MouseEvent.MOUSE_OVER, submenuOver);
menuItem.addEventListener(MouseEvent.MOUSE_OUT, submenuOut);
//Set the menuItem to have no mouseChildres
menuItem.mouseChildren = false;
//Add the item to the menuArray
menuArray.push(menuItem);
} // END for
///////////////////////////////////////////////////////////////////
//We also need to create the main MenuItem for the menu
var mainItem:MenuItem = new MenuItem();
//Set the mainItem to have no mouseChildren
mainItem.mouseChildren = false;
//Add the main item to menuArray
menuArray.push(mainItem);
//Save the array to which this mainItem belongs to.
//We need this in the animation later on.
mainItem.belongsToMenu = menuArray;
//Set the "id" attribute to be the main item's text
mainItem.menuText.text = menu. @ nume;
mainItem.buttonMode = true;
//Add CLICK listener for the mainItem
mainItem.addEventListener(MouseEvent.CLICK, mainItemClicked);
mainItem.addEventListener(MouseEvent.MOUSE_OVER, mainItemOver);
mainItem.addEventListener(MouseEvent.MOUSE_OUT, mainItemOut);
function mainItemOver(e:Event):void {
Tweener.addTween(mainItem.bg,{ _color:0x99CC00, time:0.5 , transition:"easeOutSine"});
}
function mainItemOut(e:Event):void {
Tweener.addTween(mainItem.bg,{ _color:0x333333, time:1 , transition:"easeOutSine"});
}
//Update the menuCounter since we just created a new menu
menuCounter++;
//Return the menuArray that contains all the items in this menu
return menuArray;
} // END function CreateMenu
//////////////////////////////////////////////////////////////////////
function submenuClicked(e:Event):void {
trace(" apas submenu " + MenuItem);
//Get the item that dispatched the event
var item:MenuItem = e.target as MenuItem;
//Navigate to the URL that's assigned to the menu item
var urlRequest:URLRequest = new URLRequest(item.link);
navigateToURL(urlRequest);
}
//////////////////////////////////////////////////////////////////////
function submenuOver(e:Event):void {
//Get the item that dispatched the event
var item:MenuItem = e.target as MenuItem;
Tweener.addTween(item.bg,{ _color:0x99CC00, time:0.5 , transition:"easeOutSine"});
}
//////////////////////////////////////////////////////////////////////
function submenuOut(e:Event):void {
//Get the item that dispatched the event
var item:MenuItem = e.target as MenuItem;
Tweener.addTween(item.bg,{ _color:0x333333, time:0.5 , transition:"easeOutSine"});
}
//////////////////////////////////////////////////////////////////////
//This function is called when a menu's mainItem is clicked
function mainItemClicked(e:Event):void {
//Animate the previous menu down if there is one
if (selectedMenu) {
for (var i =0; i< selectedMenu.length-1; i++) {
Tweener.addTween(selectedMenu*,{ alpha:0, x:stage.stageWidth, time:1 , transition:"easeOutSine"});
}
} //END if
//Get the menu where the mainItem is located
var clickedMenu:Array = e.target.belongsToMenu;
//Set the clickedMenu to be our selectedMenu
selectedMenu = clickedMenu;
//Loop through the items except for the last one which is the mainItem.
//We don't animate the mainItem
if (selectedMenu) {
for (var j =0; j< selectedMenu.length-1; j++) {
//Save the item to a local variable
var item = selectedMenu[j];
selectedMenu[j].alpha = 0;
selectedMenu[j].buttonMode = true; // activate the submenu HandMouse
selectedMenu[j].x = -200; // where the submenu start to fade in
selectedMenu[j].y = 45;
//Calcute the target y coordinate for the item.
//var targetY:Number = igrec - ITEM_HEIGHT * 1.2*(1 + j);
var targetX:Number = 1 + 131 * (1 + j ); // distance beatween submenu
//Tween an item up.
Tweener.addTween(item,{ alpha:1, x:targetX, time:0.5 , transition:"easeOutSine"});
}
}
} //END if
I would like to know how to asign url and to the main btn, not only for submenu, if we have an xml like this:
<menus>
<menu nume=“MAIN BTN”>
<subNume link=“http://google.com”>google</subNume>
</menu>
<menu nume=“DIRECT LINK”>
<url link=“http://yahoo.com”/>
</menu>
</menus>
I will appreciate your response, thank you.