First time here? First time here? Newsletter Signup Newsletter
651326 Members and growing! | Flash Jobs
Search tutorials
Author: Nuno Mira | Website: http://www.nunomira.com/

ActionScript

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:

package
{
  	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);
xmlloader.load(request);
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 loadImage(); }
/** * 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);
loader.load(request);
// increase the current number current++; 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); addChild(bm);
while (numChildren > 1) removeChildAt(0);
timer.start(); }
/** * load the next image * @param e */ private function timerComplete(e:TimerEvent):void { loadImage(); } } }

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:

Note:

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.

Conclusion:

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-02-28
Rating: 7.5 Votes: 2
(10 being the highest)
» 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!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
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: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.