Flash XML Gallery
Friday, February 27th, 2009This post will walk through the construction of a simple XML and Flash gallery. If you want to use the images and XML file we are referring to in this example you can download our XML document here and download our images here. Our gallery will simply load the first image and then with each click it’ll load the next image form our XML document.
First of all here is the XML we will be working with:
<?xml version="1.0" encoding="utf-8"?>
<gallery>
<photo>
<filename>http://adambenjamin.com/blog/wp-content/img_0001.jpg</filename>
<width>800</width>
<height>531</height>
<title>Field at Night</title>
</photo>
<photo>
<filename>http://adambenjamin.com/blog/wp-content/img_0002.jpg</filename>
<width>800</width>
<height>531</height>
<title>Van on the Road</title>
</photo>
<photo>
<filename>http://adambenjamin.com/blog/wp-content/img_0003.jpg</filename>
<width>800</width>
<height>531</height>
<title>Van on the Road Close Up</title>
</photo>
<photo>
<filename>http://adambenjamin.com/blog/wp-content/img_0004.jpg</filename>
<width>800</width>
<height>531</height>
<title>Mountains</title>
</photo>
<photo>
<filename>http://adambenjamin.com/blog/wp-content/img_0005.jpg</filename>
<width>800</width>
<height>531</height>
<title>Edge of a Cliff</title>
</photo>
<photo>
<filename>http://adambenjamin.com/blog/wp-content/img_0006.jpg</filename>
<width>800</width>
<height>531</height>
<title>Another Road</title>
</photo>
<photo>
<filename>http://adambenjamin.com/blog/wp-content/img_0007.jpg</filename>
<width>800</width>
<height>531</height>
<title>Tight Path</title>
</photo>
<photo>
<filename>http://adambenjamin.com/blog/wp-content/img_0008.jpg</filename>
<width>800</width>
<height>531</height>
<title>Yellow Pickup</title>
</photo>
<photo>
<filename>http://adambenjamin.com/blog/wp-content/img_0009.jpg</filename>
<width>800</width>
<height>531</height>
<title>Power Lines</title>
</photo>
<photo>
<filename>http://adambenjamin.com/blog/wp-content/img_0010.jpg</filename>
<width>800</width>
<height>531</height>
<title>Pig</title>
</photo>
<photo>
<filename>http://adambenjamin.com/blog/wp-content/img_0011.jpg</filename>
<width>800</width>
<height>531</height>
<title>Drying Coffee Beans</title>
</photo>
<photo>
<filename>http://adambenjamin.com/blog/wp-content/img_0012.jpg</filename>
<width>800</width>
<height>531</height>
<title>Homes</title>
</photo>
</gallery>
In this example the only piece of information we will need is the filename. In future posts we may use the image titles and dimensions.
First we need to import a few Classes:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
Next we need a Movie Clip to load images into and apply Tweens to:
var containerMovie:MovieClip = new MovieClip();
addChild(containerMovie);
Then we define a URL Loarder and an XML Object:
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
We are going to need two variables: one to keep track of what image we are currently viewing and another to keep track of whether or not we are in the progress of loading the next image.
var galleryCounter:Number=new Number(0);
var galleryStatus:Boolean=new Boolean(false);
Then we are gong to add an Event LIstener to the main Movie Clip to listen for Click Events.
containerMovie.buttonMode = true;
containerMovie.addEventListener(MouseEvent.CLICK,clickHandler);
When some one clicks the main Movie Clip we check to make sure another image isn’t half way through loading; and if it isn’t we add one to the counter Variable, check to make sure it hasn’t gone too high and then load that photo.
function clickHandler(evt:MouseEvent):void {
if (galleryStatus==false) {
galleryCounter++;
if (galleryCounter>=xmlData.photo.length()) {
galleryCounter=0;
}
loadPhotoStep1(galleryCounter);
}
}
Now that our containers and butons are all set up lets load our XML.
xmlLoader.load(new URLRequest("gallery.xml"));
Once that loading is complete we put the loaded information into our XML Object and start eh first step of loading our image.
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
function LoadXML(e:Event):void {
xmlData=new XML(e.target.data);
loadPhotoStep1(galleryCounter);
}
Setp 1 - we save the loaded image number into our galleryCounter variable and set galleryStatus to true. This prevents our clickHandler function above from loading another image before this one has finished loading. And we Tween the Alpha out of the current image.
function loadPhotoStep1(photoNumber:Number):void {
galleryCounter=photoNumber;
galleryStatus=true;
var tweenOut:Tween=new Tween(containerMovie,"alpha",Strong.easeOut,1,0,15);
tweenOut.addEventListener(TweenEvent.MOTION_FINISH,loadPhotoStep2);
}
Step 2 - once the first Tween has completed we load empty the container Movie Clip and load in the new image.
function loadPhotoStep2(evt:TweenEvent):void {
var imageLoader:Loader = new Loader();
var imageRequest:URLRequest=new URLRequest(xmlData.photo[galleryCounter].filename);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadPhotoStep3);
imageLoader.load(imageRequest);
emptyContainer();
containerMovie.addChild(imageLoader);
}
One the image has loaded in we Tween the Alpha back in.
function loadPhotoStep3(evt:Event):void {
var tweenIn:Tween=new Tween(containerMovie,"alpha",Strong.easeOut,0,1,15);
tweenIn.addEventListener(TweenEvent.MOTION_FINISH,loadPhotoStep4);
}
Once that Tween is complete we set galleryStatus back to false so the next time the user clicks the Movie Clip i’s good to go!
function loadPhotoStep4(evt:Event):void {
galleryStatus=false;
}
This last function removes all Children from the containerMovie Movie Clip. It is called by the step 2 function.
function emptyContainer() :void {
for (var i:int; i < containerMovie.numChildren; i++){
containerMovie.removeChildAt(i);
}
}



