Multidimensional array, loop of buttons, and loading swf/img/txt from xml
Okay, here goes: What I'm trying to do is create a multidimensional array which will pull text, swfs, and/or images from an xml file based on which button is clicked.
What I don't understand is how to pull the swf or image file from the file like I have with the text. Truth be told, I'm completely lost and could really use some help fixing this. I need to pull text and images or the swf file from the xml depending on which button is clicked and I tried to put this together, but I've given myself a headache trying to understand where I went wrong.
var pageXML:XML;
//Load the XML file.
var pageXMLLoader = new URLLoader();
pageXMLLoader.load(new URLRequest("scripts/xml/content.xml"));
pageXMLLoader.addEventListener(IOErrorEvent.IO_ERROR, xmlIOErrorHandler);
pageXMLLoader.addEventListener(Event.COMPLETE, pageXMLLoaded);
// Intro page variables for title
var swfContainer:Sprite = new Sprite();
var imgContainer:Sprite = new Sprite();
var imgLoader:Loader = new Loader();
var swfLoader:Loader = new Loader();
//This function is called when the XML file is loaded
function pageXMLLoaded(e:Event):void {
//Create a new XML object from the loade开发者_JS百科d XML data
pageXML = new XML(e.target.data);
imgLoader.load(new URLRequest(pageXML.pages.page.image));
swfLoader.load(new URLRequest(pageXML.pages.page.swfURL));
trace(pageXML.pages.page.image);
trace(pageXML.pages.page.swfURL);
}
// Choose page transition function
function choosePageTransition(e:MouseEvent):void {
var pageNumber:uint;
mcContent.y = 30; // Why do I need to reset the y position each time?
// Set up if for mouse click
if (e.type == MouseEvent.CLICK) {
// Switch based on target name
switch (e.currentTarget.name) {
case "navBtn0" :
trace(e.currentTarget.name);
pageNumber = 1;
mcContent.removeChildAt(0);
mcContent.addChildAt(pageContent,0);
pageContent.addChild(contentRBottomTxt);
pageContent.addChild(contentRTopTxt);
pageContent.addChild(contentTxt);
pageContent.addChild(headingTxt);
break;
case "navBtn1" :
trace(e.currentTarget.name);
pageNumber = 2;
mcContent.removeChildAt(0);
mcContent.addChildAt(pageContent,0);
pageContent.addChild(contentRBottomTxt);
pageContent.addChild(contentRTopTxt);
pageContent.addChild(contentTxt);
pageContent.addChild(headingTxt);
break;
case "navBtn2" :
trace(e.currentTarget.name);
pageNumber = 3;
mcContent.removeChildAt(0);
mcContent.addChildAt(pageContent,0);
pageContent.addChild(contentRBottomTxt);
pageContent.addChild(contentRTopTxt);
pageContent.addChild(contentTxt);
pageContent.addChild(headingTxt);
break;
... code shortened
}
// Loop through the XML file
for each (var page:XML in pageXML.pages.page) {
// "page.@pagenumber" points to "pagenumber" in the XML file.
if (page.@ pagenumber == pageNumber) {
// Set the title
headingTxt.htmlText = page.title;
// Set the page content
contentTxt.htmlText = page.content;
contentRTopTxt.htmlText = page.contenttr;
contentRBottomTxt.htmlText = page.contentbr;
// Exit the loop
break;
}
} // < /For Loop >
} // < /If Statement >
} // < /Function >
My xml file looks somewhat like this (it's long, so I'll post a partial):
<?xml version="1.0" encoding="utf-8"?>
<site>
<!--<sitetitle><![CDATA[<strong>Transplant the Heart</strong>]]></sitetitle>-->
<pages>
<page pagenumber="1">
<title><![CDATA[<h1>Page #1</h1>]]></title>
<content><![CDATA[<p>Aliquam nec dui neque. Aenean rutrum suscipit turpis vel fringilla. </p>]]></content>
<contenttr><![CDATA[Cras non risus nisi. Nam tincidunt lacinia massa, vel interdum massa ultricies.]]></contenttr>
<contentbr><![CDATA[Aliquam erat volutpat. Etiam eget mauris ante, sit amet placerat tortor.]]></contentbr>
<image>imgs/png/titleLarge.png</image>
<swfURL>transplantSpecialists.swf</swfURL>
</page>
</pages>
</site>
Are you sure you have the correct structure? Something like this ...
project folder
| main.swf
| scripts/
| xml/
| content.xml
| imgs/
| png/
| titleLarge.png
| transplantSpecialists.swf
精彩评论