PDA

View Full Version : adding XML-generated URL to a button


marilynn.fowler
07-14-2008, 08:28 AM
I am using an external XML file to generate a slideshow. When I click on a thumbnail a larger image appears.
I have a button on the main stage called pdf_btn. I would like the pdf_btn link to update the URL when I click the thumbnail so that if I were to click pdf_btn it would go to the appropriate PDF. How do I do that?

Here's my code:
//import Tween classes
import fl.transitions.Tween;
import fl.transitions.easing.*;

var fadeTween:Tween;

//to load images, you need to use the Loader class
var thumbLoader:Loader;
var photoLoader:Loader

//creates xml object that holds loaded data
var xml:XML;
//treats it as an array
var xmlList:XMLList;
//when loading xml, use URLLoader class
var xmlLoader:URLLoader = new URLLoader();
//set an initial value for PDF first frame
var pdfLink:URLRequest = new URLRequest("pdf/zen2.pdf");

//loads the xml file
xmlLoader.load(new URLRequest("data/lamps_table.xml"));
//add event listener to execute function when file has completely loaded
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

function xmlLoaded(event:Event): void
{
//converts data to XML data
xml = XML(event.target.data);
//set the value of the xmlList variable
xmlList = xml.children();

//create loop to load all of the thumbnails, get path from xml file
for (var i:int = 0; i < xmlList.length(); i++)
{
//make imageLoader new instance of Loader class
thumbLoader = new Loader();
//load all of the thumbnails inside of xml file using URLRequest to load file
thumbLoader.load(new URLRequest(xmlList[i].attribute("thumb")));

//set x position from left
thumbLoader.x = 660;
//starts 5 pixels from top
thumbLoader.y = i * 65 + 5;
//name property refers to instance name. Since we're not using it, set it to "source" value
thumbLoader.name = xmlList[i].attribute("source");
//put imageLoader on stage using addChild
addChild(thumbLoader);
//execute function when mouse is clicked
thumbLoader.addEventListener(MouseEvent.CLICK, showPhoto);
}
}

function showPhoto(event:MouseEvent):void
{
/*Connect the fullsize image to thumbnail by using the name property of the imageLoader
to hold the path to its fullsize image
*/
photoLoader = new Loader();
photoLoader.load(new URLRequest(event.target.name));
photoLoader.x = 1;
photoLoader.y = 0;
addChild(photoLoader);

//object to fade, alpha property, no function, starting opacity, ending opacity, seconds, true
fadeTween = new Tween(photoLoader, "alpha", None.easeNone, 0, 1, 1, true);

for(var j:int = 0; j < xmlList.length(); j++)
{
if(xmlList[j].attribute("source") == event.target.name)
{
SKU_txt.text = xmlList[j].iSKU;
name_txt.text = xmlList[j].iName;
materials_txt.text = xmlList[j].iMaterials;
height_txt.text = xmlList[j].iHeight;
base_txt.text = xmlList[j].iBase;
shadeSKU_txt.text = xmlList[j].iSKU + "s";
shade_txt.text = xmlList[j].iShade;
shadeMaterials_txt.text = xmlList[j].iShadeMaterials;
dimensions_txt.text = xmlList[j].iDimensions;

trace(xmlList[j].attribute("pdf"));

}
}
}

pdf_btn.addEventListener(MouseEvent.CLICK, getPDF);

function getPDF(event:MouseEvent):void
{
navigateToURL(pdfLink);
}

marilynn.fowler
07-14-2008, 01:06 PM
I figured it out by trial and error. YAY! In the loop of the showPhoto function I had to set the pdfLink variable and put in the event listener. For anyone who is interested, here is the code:


//import Tween classes
import fl.transitions.Tween;
import fl.transitions.easing.*;

var fadeTween:Tween;

//to load images, you need to use the Loader class
var thumbLoader:Loader;
var photoLoader:Loader

//creates xml object that holds loaded data
var xml:XML;
//treats it as an array
var xmlList:XMLList;
//when loading xml, use URLLoader class
var xmlLoader:URLLoader = new URLLoader();
//set an initial value for PDF first frame
var pdfLink:URLRequest = new URLRequest("pdf/egyptian.pdf");

//loads the xml file
xmlLoader.load(new URLRequest("data/lamps_floor.xml"));
//add event listener to execute function when file has completely loaded
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

function xmlLoaded(event:Event): void
{
//converts data to XML data
xml = XML(event.target.data);
//set the value of the xmlList variable
xmlList = xml.children();

//create loop to load all of the thumbnails, get path from xml file
for (var i:int = 0; i < xmlList.length(); i++)
{
//make imageLoader new instance of Loader class
thumbLoader = new Loader();
//load all of the thumbnails inside of xml file using URLRequest to load file
thumbLoader.load(new URLRequest(xmlList[i].attribute("thumb")));

//set x position from left
thumbLoader.x = 665;
//starts 5 pixels from top
thumbLoader.y = i * 65 + 5;
//name property refers to instance name. Since we're not using it, set it to "source" value
thumbLoader.name = xmlList[i].attribute("source");
//put imageLoader on stage using addChild
addChild(thumbLoader);
//execute function when mouse is clicked
thumbLoader.addEventListener(MouseEvent.CLICK, showPhoto);
}
}

function showPhoto(event:MouseEvent):void
{
/*Connect the fullsize image to thumbnail by using the name property of the imageLoader
to hold the path to its fullsize image
*/
photoLoader = new Loader();
photoLoader.load(new URLRequest(event.target.name));
photoLoader.x = 1;
photoLoader.y = 0;
addChild(photoLoader);

//object to fade, alpha property, no function, starting opacity, ending opacity, seconds, true
fadeTween = new Tween(photoLoader, "alpha", None.easeNone, 0, 1, 1, true);

for(var j:int = 0; j < xmlList.length(); j++)
{
if(xmlList[j].attribute("source") == event.target.name)
{
SKU_txt.text = xmlList[j].iSKU;
name_txt.text = xmlList[j].iName;
materials_txt.text = xmlList[j].iMaterials;
height_txt.text = xmlList[j].iHeight;
base_txt.text = xmlList[j].iBase;
shadeSKU_txt.text = xmlList[j].iSKU + "s";
shade_txt.text = xmlList[j].iShade;
shadeMaterials_txt.text = xmlList[j].iShadeMaterials;
dimensions_txt.text = xmlList[j].iDimensions;
//reset pdfLink with new URL
pdfLink = new URLRequest(xmlList[j].attribute("pdf"));
pdf_btn.addEventListener(MouseEvent.CLICK, getPDF);
}
}
}

function getPDF(event:MouseEvent):void
{
navigateToURL(pdfLink);
}