XML and loading images
Part 1
Download the xml file and the two pictures image1 and image2
Open Flash and create a new flash file.
Create a dynamic text box on the stage and give it an instance name of tImages. This will be used to display the total number of images in the XML file.
Create a new layer name it actions and enter the following code:
var gallery_xml:XML;
var imagetoLoad:String;
var ImageRequest:URLRequest;
var imageLoader:Loader;
var xmlReq:URLRequest = new URLRequest("gallery.xml");
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(xmlReq);
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
function xmlLoaded(event:Event):void{
gallery_xml=new XML(xmlLoader.data);
imagetoLoad=gallery_xml.image[0].@file;
loadImage();
updateStage();
}
function updateStage():void{
tImages.text = gallery_xml.image.length();
}
We start by creating a number of variables and the ecvents for loading in th xml date. Then we create the xmloaded function which takes the xml data and assigns it to the variable gallery_xml. The next line extracts the the file name of the first image that is required to be loaded and assigns it to the variable imagetoLoad. Following this two functions are called loadImage() and updateStage(). The updateStage displays the number of images in the gallery in this case 2.
Exercise
Part 1
Using the previous tutorials see if you can create the loadImage function so that the Image is loaded and displayed on the stage. You will need to use the imagetoLoad variable to get the image details.
function loadImage():void{
// code for loading the image
}
Part 2
See if you can create a button that when clicked will load the next image in. Hint you will still need to alter the value stored in imagetoLoad.
