SQL-driven AS3 Menu

ActionScript Code

Requires the Greensock tweenlite framework, inside the GS folder. Download the zip file using the link below.

as3_xml_sql_menu.fla

import gs.*;

	var xmlLoader:URLLoader = new URLLoader();
	xmlLoader.addEventListener(Event.COMPLETE, showXML);
	xmlLoader.load(new URLRequest("sqlMenu.php"));
	function showXML(e:Event):void {
		XML.ignoreWhitespace = true;
		var menu:XML = new XML(e.target.data);
		var i:Number;
		var j:Number;
		var k:Number;
		for (i=0; i < menu.section.length(); i++) {
			var mc:MovieClip = new MovieClip();
			var mc1:MovieClip = new MovieClip();
			var t:TextField = new TextField();
			t.y = 0;
			t.x = 0;
			t.selectable = true;
			t.width = 200;
			t.height = 80;
			t.htmlText = ""+ menu.section[i].text() +"";
			t.setTextFormat(new TextFormat("Arial", 22));
			t.rotationZ = 0;
			t.autoSize = TextFieldAutoSize.LEFT;
			mc.addChild( t );
			addChild(mc);
			mc.x = 200*i;
			mc.addEventListener(MouseEvent.MOUSE_OVER, showSection);
			mc.addEventListener(MouseEvent.MOUSE_OUT, hideSection);
			for (j=0; j < menu.section[i].subsection.length(); j++) {
				var mc2:MovieClip = new MovieClip();
				var t2:TextField = new TextField();
				t2.y = 0;
				t2.x = 0;
				t2.selectable = true;
				t2.width = 45;
				t2.height = 100;
				t2.htmlText = ""+ menu.section[i].subsection[j].text() +"";		var f2:TextFormat = new TextFormat();
				f2.align = TextFormatAlign.CENTER;
				f2.size = 14;
				f2.font = "Arial";
				t2.setTextFormat(f2);
				t2.rotationZ = 0;
				t2.autoSize = TextFieldAutoSize.CENTER;
				mc2.addChild( t2 );
				mc.addChild(mc2);
				mc2.x = 50*j;
				mc2.y = 30;
				for (k=0; k < menu.section[i].subsection[j].subsubsection.length(); k++) {
					var mc3:MovieClip = new MovieClip();
					var t3:TextField = new TextField();
					t3.y = 0;
					t3.x = 0;
					t3.selectable = true;
					t3.width = 60;
					t3.height = 20;
					t3.htmlText = ""+ menu.section[i].subsection[j].subsubsection[k].text() +"";
					t3.setTextFormat(new TextFormat("Arial", 12));
					t3.rotationZ = 0;
					mc3.addChild( t3 );
					mc2.addChild(mc3);
					mc3.x = 0;
					mc3.y = 20*k+20;
				}//subsection[j].subsubsection.length()
				for (l=1; l < mc2.numChildren; l++) {
					mc2.getChildAt(l).alpha = 0;
					mc2.addEventListener(MouseEvent.MOUSE_OVER, showSubsection);
					mc2.addEventListener(MouseEvent.MOUSE_OUT, hideSubsection);
				}//for (l=1; l < mc2.numChildren; l++)
				}//(j=0; j < menu.section[i].subsection.length(); j++)
				var l:Number;
				for (l=1; l < mc.numChildren; l++) {
					mc.getChildAt(l).alpha = 0;
				}
		}//for (i=0; i < menu.section.length(); i++)
	}//function showXML(e:Event):void
	function showSection(e:MouseEvent) {
		var mc:MovieClip = e.currentTarget as MovieClip;
		var l:Number;
		for (l=1; l < mc.numChildren; l++) {
			TweenLite.to(mc.getChildAt(l), .5, {alpha:1});
		}
	}
	function hideSection(e:MouseEvent) {
		var mc:MovieClip = e.currentTarget as MovieClip;
		var l:Number;
		for (l=1; l < mc.numChildren; l++) {
			TweenLite.to(mc.getChildAt(l), .2, {alpha:0});
		}
	}
	function showSubsection(e:MouseEvent) {
		var mc:MovieClip = e.currentTarget as MovieClip;
		var l:Number;
		for (l=1; l < mc.numChildren; l++) {
			TweenLite.to(mc.getChildAt(l), .5, {alpha:1});
		}
	}
	function hideSubsection(e:MouseEvent) {
		var mc:MovieClip = e.currentTarget as MovieClip;
		var l:Number;
		for (l=1; l < mc.numChildren; l++) {
			TweenLite.to(mc.getChildAt(l), .2, {alpha:0});
		}
	}

PHP Code

Paste into a text editor and save as sqlMenu.php in the same folder as your SWF. PHP files need to be viewed on a webserver to function. This PHP file is grabbing information from your database and formatting it as XML.

In this example, the MySQL database server is localhost, and the username and password are both root. Also, the database name is fashion and has the tables menu, submenu, and subsubmenu. The menu table has two columns: id and section. The submenu table has three columns: id, section and subsection. The subsubmenu table has four columns: id, section, subsection and subsubsection. Filling out these fields appropriately will yield a functioning hierarchical menu in the Flash SWF file. Download the zip below.

sqlMenu


AS3 Function to Encode Spaces into a URL

function encodespaces(originalstring:String):String
{
var original:Array=originalstring.split(" ");
return(original.join("%20"));
}


Vitamate: The Vitamin & Mineral Reference App for the iPhone


Claiming this blog on technorati

Technorati Profile


New iPhone Application: iAdmit

http://appshopper.com/entertainment/iadmit

iAdmit_iPhone_iPod_App-store_01

iAdmit-icon-in-the-appstore


Brainbench Photoshop Certification

Adobe Photoshop CS3
Score: 4.69
Percentile: Scored higher than 99% of previous examinees

Demonstrates understanding of most advanced concepts within the subject area. Appears capable of mentoring others on the most complex projects.
Strong Areas
  • Painting and Drawing
  • Retouching
  • Selecting
  • Layers
  • Color Management
Weak Areas
  • None noted


Logo for Children’s Book Character

Neanderthal Ned Logo