I was presented with a challenge by a client to produce a Flash based, lightweight slideshow for use on a website. This slideshow would be able to read the number of images in a directory on a webserver and cycle through the images based on the number of images present. Futhermore, it would be able to detect and adapt itself when there was a new image or image removed WITHOUT the need to adjust and re-compile Flash code.
During my initial searches on the internet, it seemed that the only way to do this would be to use a PHP script to regulate the information sent to AS3. The consensus on the internet was that it was not possible to detect the number of files present in a web directory. I did figure out a way to do this.
Here is the basic approach I took to solving this problem. Flash can not read what files are in a directory, but it can pull a file with a specific name. If the file doesn’t exist then it will throw an exception. So what I ended up doing was create a naming convention (eg: photo01.jpg, photo02.jpg, etc.) for the pictures that would be loaded in. Flash then goes through a loop archiving the valid file names and stops on the first null. I then make a pass starting backwards and eliminate any null entries in my array and preload the photo’s that are valid. To hide the time it takes to do this, I show the first valid file in the slideshow when it does the first scan. Then we start our slideshow loop in reverse, it takes about the same amount of time to load the photos as it did to swap photos, so this masks the loading well.
This approach was certainly not the fastest or most efficient, but on a time crunch it worked well. If I ever need to do it again for a client, I have a strong starting point and can optimize the code.
