Main class
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
/*
* Main Class
* Document class for movie
*/
public class Main extends Sprite
{
private var myMenu:Component;
private var xmlData:XML;
public function Main ():void
{
/*
* We load the xml file, which contains all the data to create the menu.
*/
xmlData = new XML();
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load (new URLRequest("menu.xml"));
xmlLoader.addEventListener (Event.COMPLETE, xmlLoaded);
}
/*
* When the xml file is loaded we create the menu.
*/
private function xmlLoaded (event:Event):void
{
xmlData = XML(event.target.data);
/*
* We first create the main holder for the menu, which is a Component
* and position it. We add all the MenuItems then to the main menu.
*/
myMenu = new Component();
myMenu.x = 25;
myMenu.y = 25;
addChild (myMenu);
/*
* We cycle through the xml file.
*/
for (var i:int = 0; i < xmlData.menuitem.length(); i++)
{
var mItem:Component;
/*
* Headline for the MenuItems.
*/
var mainDescription:String = xmlData.menuitem[i].attribute("title");
var array:Array = new Array();
/*
* Only when the menu has children we create an array to store the
* data for the children. Otherwise the array will be null but the
* third parameter for a URL or anything else we want to do will be
* stored.
*/
var subLength:int = xmlData.menuitem[i].menusubitem.length();
if (subLength > 0)
{
for (var j:int = 0; j < xmlData.menuitem[i].menusubitem.length(); j++)
{
array.push ({headLine:xmlData.menuitem[i].menusubitem[j].attribute("title"),_url:xmlData.menuitem.menusubitem[j].toString()});
}
mItem = new MenuItem(array,mainDescription,null);
}
else
{
var theURL:String = xmlData.menuitem[i].toString();
mItem = new MenuItem(null,mainDescription,theURL);
}
mItem.y = 30 * i;
myMenu.addChild (mItem);
}
}
}
}