ActionScript 3 – Dynamic Menu
Here I’ll be showing you how to create a menu in Flash that creates a navigation structure from an XML file. Each button can load various types of media (images, swfs, videos) or even open a web page. We will open Flash and create a new Movie Clip called “nav” and give it the same linkage id class name. Create a a box 40px tall and convert it to a Movie Clip with the instance name of “bg”. Create a new layer and create a new dynamic text field with the instance name of “title_txt”. Make sure to embed your fonts for your desired character set. Now here is one thing I figured out that is kind of important. Create another layer on top and copy your MC called “bg” and paste it into this new layer with the instance name of “trans” and set the Alpha property to zero. This will act as our hit state. Without this the cursor loses focus when it is located over the dynamic text.
Create a class file called “BuildMenu.as”. In Flash, change your Document Class to BuildMenu. Save.
Create an XML file called “menu.xml” and fill it with these contents:
<?xml version="1.0" encoding="utf-8"?> <navigation> <button title="Image" action="image1.jpg" /> <button title="Video F4V" action="video1.f4v" /> <button title="Video MPEG" action="video2.mpeg" /> <button title="SWF File" action="swf1.swf" /> <button title="Contact" action="http://www.google.com" /> </navigation>
Open BuildMenu.as and fill it with this code:
// ActionScript file package { import fl.transitions.Tween; import fl.transitions.easing.*; import flash.display.*; import flash.events.*; import flash.net.*; public class BuildMenu extends Sprite { private var titles:Array = new Array(); private var actions:Array = new Array(); private var total:uint = 0; private var xmlFile:String = "menu.xml"; private var loadMenuXML:URLLoader = new URLLoader(); private var fadeSpeed:Number = 0.2; public function BuildMenu() { var reqMenuXML:URLRequest = new URLRequest(xmlFile); loadMenuXML.addEventListener(Event.COMPLETE, parseXML); loadMenuXML.load(reqMenuXML); } private function parseXML(e:Event):void { var menuXML:XML = new XML(loadMenuXML.data); var container:Sprite = new Sprite(); container.name = "container"; container.x = 100; addChild(container); for (var i:int = 0; i<menuXML.*.length(); i++) { titles.push(menuXML.button[i].@title); actions.push(menuXML.button[i].@action); var btn:MovieClip = new nav; container.addChild(btn); btn.y = i*40; btn.title_txt.htmlText = titles[i]; btn.trans.addEventListener(MouseEvent.CLICK, navClick); btn.trans.addEventListener(MouseEvent.ROLL_OVER, fadeDown); btn.trans.addEventListener(MouseEvent.ROLL_OUT, fadeUp); btn.trans.buttonMode = true; btn.trans.ID = i; } } private function fadeDown(e:Event):void { var fDown:Tween = new Tween(e.currentTarget.parent.bg, "alpha", None.easeOut, e.currentTarget.parent.bg.alpha, 0.5, fadeSpeed, true); } private function fadeUp(e:Event):void { var fUp:Tween = new Tween(e.currentTarget.parent.bg, "alpha", None.easeOut, e.currentTarget.parent.bg.alpha, 1, fadeSpeed, true); } private function navClick(e:Event):void { determineLink(e.currentTarget.ID); } private function determineLink(id) { //trace(titles[id]+" -> "+actions[id]); var actionItem:String = actions[id].toString(); // Find file extension var fileExtension:String = actionItem.substr(0,4); if (fileExtension == "http") { // this is a URL } else { fileExtension = actionItem.substr(actionItem.lastIndexOf('.')+1, actionItem.length); // this is a file extension } var mediaType:String = fileExtension.toLowerCase(); switch (mediaType) { case "http": trace("Open URL -> "+actionItem); openURL(actionItem); break; case "flv": case "f4v": case "mov": case "mpeg": case "mp4": trace("Load video -> "+actionItem); loadVideo(actionItem); break; case "jpg": case "gif": case "jpeg": case "png": trace("Load image -> "+actionItem); loadImage(actionItem); break; case "swf": trace("Load swf -> "+actionItem); loadSWF(actionItem); break; default : trace("Incorrect file type used"); break; } } private function loadImage(file) { // load Image var assetLoader:Loader = new Loader(); addChild(assetLoader); assetLoader.load(new URLRequest(file)); } private function loadSWF(file) { // load SWF var assetLoader:Loader = new Loader(); addChild(assetLoader); assetLoader.load(new URLRequest(file)); } private function loadVideo(file) { // load Video } private function openURL(url) { // open URL var loadURL:URLRequest = new URLRequest(url); try { navigateToURL(loadURL, "_blank"); } catch (e:Error) { trace("Cannot load -> "+url); } } } }
To view the output, Test Movie in Flash.

