Loading/parsing XML
We are now ready to load the XML file, for which we use the TextLoader class. One of the advantages of using these classes is that we don't have to import all the classes necessary for loading a file, neither we need to care about the event listeners. All we do is initiate the TextLoader class. In the listener function we have to add 3 variables but not care any further. The function is created in that way that we can pass different types of variables, of which here however we don't make any use.
var myXML:TextLoader = new TextLoader();
myXML.initText ("menu.xml",completeHandler);
function completeHandler (e:Event,a:String,b:Number,c:Boolean):void
{
Once the file is loaded we retrieve the data.
var myXML:XML=new XML(e.currentTarget.data);
We cycle through the nodes of the XML file. We use the Timer class to have more control of loading the thumbs.
var count01:Number = 0; var myTimer:Timer = new Timer(400,myXML.sub.length()); myTimer.addEventListener (TimerEvent.TIMER, loadingThumbs); myTimer.start (); function loadingThumbs (e:TimerEvent):void {
We create holders for the thumbs and position them. These have to be of the MovieClip class, since we associate values with the variables later.
var _mc:MovieClip = new MovieClip(); _mc.name="_mc"+count01; _mc.x = (75 * count01) + 40; _mc.y = 340; _mc.alpha = 0; _mc.buttonMode=true;
We get the URL and name of the slides by just searching for the sub nodes in the DOM and getting the attributes. We associate the variables for the attribute values with the thumb MovieClips.
var slideURL:String=myXML.sub.attribute("slide")[count01]; var slideName:String=myXML.sub.attribute("name")[count01]; _mc.slideURL=slideURL; _mc.slideName=slideName;
We create an array with those data. Then we add the thumbs to the DisplayList.
imageArray.push ({name:slideName, data:slideURL}); addChild (_mc);
To add the images to the thumbs we use the LoaderClass with parameters for the URL, the listener function and the target for the Loader, _mc.
var lc:LoaderClass=new LoaderClass(); lc.initLoader (slideURL,loadingComplete,_mc); count01++; } }
Once loading of the thumbs is complete we need to resize them to a smaller size. We also need to make them visible again, when loading and sizing is complete. Finally we add button events to each of the thumbs.
function loadingComplete (ev:Event,a:Object,b:Object,c:Object):void
{
var mc:MovieClip=ev.currentTarget.content.parent.parent;
mc.scaleX=0.2;
mc.scaleY=0.2;
mc.alpha = 1;
mc.addEventListener (MouseEvent.CLICK,mouseHandler);
}
In the next section we are ready to show individual slides.