ActionScript
First you load the XML file.
As weve 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 images 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!
Youve 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 |
» 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. |
Download (814 kb) |
» Forums |
More help? Search our boards for quick answers! |