The XML
<data>
<button handle=”prim_butt1″ text=”Larry” x=”60″ y=”160″ ></button>
<button handle=”prim_butt2″ text=”Moe” x=”170″ y=”160″ ></button>
<button handle=”prim_butt3″ text=”Curly” x=”280″ y=”160″ ></button>
<button handle=”prim_butt4″ text=”Shemp” x=”390″ y=”160″ ></button>
</data>
Now The Actionscript:
package {
import flash.display.MovieClip;
import flash.events.*;
import flash.net.*;
import flash.text.*;
import mc_button_primary;
public class StarterClass extends MovieClip {
private var i:Number;
private var primButtonCount:Number;
private var xmlLoader:URLLoader;
private var dataXML:XML;
private var buttonPrimary:mc_button_primary;
private var urlArray = new Array();
public function StarterClass() {
trace(“StarterClass……….√ check”);
// create a loader for our xml file
xmlLoader = new URLLoader(new URLRequest(“nav_primary.xml”));
// oops couldn't get our xml file
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, noFile);
// got our file now go do something (myInitMethod) with it
xmlLoader.addEventListener(Event.COMPLETE, myInitMethod);
// THROW ERROR IF XML SHOOTS CRAPS
function noFile(evt:IOErrorEvent):void {
trace(“Error. Data file not found.”);
}
}
// and now to do something with our data
private function myInitMethod(evt:Event):void {
trace(“myInitMethod……….√ check”);
// make sure flash knows it xml
dataXML = XML(xmlLoader.data);
// number of buttons in our xml
primButtonCount = dataXML.button.length();
// loop thru each button and make the data useful
for (i=0; i< primButtonCount; i++) {
trace("foo > “+dataXML.button[i].@handle );
// this is an array that contains the button text and it looks like
// urlArray[”prim_butt1″] == “Larry”
// it's referenced it in the click event
urlArray[dataXML.button[i].@handle] = dataXML.button[i].@text;
// create a new button from the library object
buttonPrimary = new mc_button_primary();
// give it a name
buttonPrimary.name = dataXML.button[i].@handle.toString();
// put it on the stage
addChild( buttonPrimary );
// find our new named button and give it a handle
var bttn_handle = getChildByName( buttonPrimary.name );
// now use the handle to move it and add text
bttn_handle.x = dataXML.button[i].@x;
bttn_handle.y = dataXML.button[i].@y;
bttn_handle.button_Text.text = dataXML.button[i].@text;
// add a listener so you can do something when you click it
bttn_handle.addEventListener( MouseEvent.CLICK, myButton_CLICK );
}
}
// here is the click event
function myButton_CLICK(event:MouseEvent):void {
// the name comes fom the urlArray ceated above
trace( “The button clicked was > “+urlArray[ event.target.name ]);
}
}
}
StarterClass_xml_basic.zip
Actionscript 3 adding simple xml nav to the starter class
Apr, 25 -- Categories: Actionscript