First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Nuno Mira | Website: http://www.nunomira.com/


First you load the XML file.
As we’ve seen before, you can load the static file directly - images.xml - or you can load the PHP script - list-files.php.
After the XML data loads you store it as an XMLList object for easier access to the image’s path:

var img:String = xmlList[current].@src;
You also store the total number of images.

total = xmlList.length();
Then you can start loading and displaying the images, using the Timer Class for the repetitive loadImage() calls:

  	import flash.display.Bitmap;
  	import flash.display.Loader;
  	import flash.display.Sprite;
  	import flash.events.Event;
  	import flash.events.TimerEvent;
  	import flash.net.URLLoader;
  	import flash.net.URLRequest;
  	import flash.utils.Timer;  
  	public class Main extends Sprite
  		// path to the xml file (it could be to the php script list-files.php)
          private var url:String = "images.xml";
          // XMLList object with the image names
          private var xmlList:XMLList;
          // URLLoader to load the XML file
          private var xmlloader:URLLoader;
          // Loader to load the images
          private var loader:Loader;  
          // number of the current image
          private var current:uint;
          // total number of images
          private var total:uint;
          // timer to load a new image
          private var timer:Timer;
          private var delay:uint = 3000; // 3 seconds  
          public function Main():void
              // make sure the stage property exists
              if (stage) init();
              else addEventListener(Event.ADDED_TO_STAGE, init);
          * load the xml
          * @param	e
          private function init(e:Event = null):void 
              var request:URLRequest = new URLRequest(url);  
              xmlloader = new URLLoader();
              xmlloader.addEventListener(Event.COMPLETE, loadXMLComplete);          
              timer = new Timer(delay, 1);
              timer.addEventListener(TimerEvent.TIMER, timerComplete);
          * when the xml finishes loading
          * store the images in an XMLList object
          * @param	e
          private function loadXMLComplete(e:Event):void 
              var xml:XML = XML(e.currentTarget.data);
              xmlList = xml.img;          
              xmlloader.removeEventListener(Event.COMPLETE, loadXMLComplete);          
              // initialize current and get the total number of images
              current = 0;
              total = xmlList.length();          
              // start loading images
          * load the image
          private function loadImage():void
              loader = new Loader();
              var img:String = xmlList[current].@src;
              var request:URLRequest = new URLRequest(img);
              loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImgComplete);
              // increase the current number
              if (current >= total)
              current = 0; // reset the current number
          * when the image finishes loading
          * add it to the display list and remove the previous one
          * start counting to load the next one
          * @param	e
          private function loadImgComplete(e:Event):void 
              var bm:Bitmap = Bitmap(loader.content);
              while (numChildren > 1)
          * load the next image
          * @param	e
          private function timerComplete(e:TimerEvent):void 

This is it!
You’ve just created a very rough slideshow.

The Document Class Main.as actually contains a little addition: a fadeIn() function that makes better looking transitions.

You can download all the files here:


As the XML file may be updated regularly, in order to prevent it from being cached, in which case it would prevent a retuning visitor from viewing all the most recent images, consider appending an unique string to the URL of the file, so that a new file is loaded every time. This technique is known as using a “cache killer”.

var url:String = "images.xml?ck=” + new Date().getTime();

Notice that this will work on a server only, i.e., it will have to be an http call.


You should now be able to generate an XML file dynamically that lists the contents of a directory using PHP, either creating a static XML file or outputing its content directly. You should be able to filter the available files by extension.
You should be able to load this XML file in Flash and parse it in order to load the corresponding images.

» Level Intermediate

Added: 2011-03-17
Rating: 1 Votes: 2
» Author
Nuno Mira has been a Flash Developer for 9 years. He loves teaching, and learning. When he isn't coding he may be surfing or snowboarding.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.