Tutorials
FILE CATEGORIES
Tutorials >> XML Image Slideshow with Preloader
DOWNLOAD THE SOURCE FILE FOR THIS TUTORIAL
Posted by webzo

We are going to make a simple slideshow that loads images and sets the time between slides from an xml file.
Create a new Movie Clip called "Bar", this is going to be our progress bar between slides.
Draw a bar like this:

Drag the Movie Clip to the stage and set the instance name to "Bar".
Now drag a loader component and make it the same size of the stage. Instance name "imgLoader".

Create an xml file in the notepad that looks like this (remember that the XML file has to be in UTF-8 encoding):
Go back to the flash, open the actions for the first frame and paste this:
Code Explanation
We need this to be a number because we will use it on our setInterval().
Posted by webzo

We are going to make a simple slideshow that loads images and sets the time between slides from an xml file.
Create a new Movie Clip called "Bar", this is going to be our progress bar between slides.
Draw a bar like this:

Drag the Movie Clip to the stage and set the instance name to "Bar".
Now drag a loader component and make it the same size of the stage. Instance name "imgLoader".

Create an xml file in the notepad that looks like this (remember that the XML file has to be in UTF-8 encoding):
- Code: Select all
<xml>
// the time between slides
<interval time = "2000" />
//path to the first image.
<image path = "1.jpg" />
//you can put as much images as you want.
<image path = "2.jpg" />
<image path = "3.jpg" />
<image path = "4.jpg" />
<image path = "5.jpg" />
</xml>
Go back to the flash, open the actions for the first frame and paste this:
- Code: Select all
//new XML object to load our xml file
var img_xml = new XML()
//ignore white to prevent problems
img_xml.ignoreWhite = true;
//load the xml
img_xml.load("images.xml");
//new array to store the path to the images
var imgArray = new Array();
//when the xml load this is called
img_xml.onLoad = function()
{
//get the timer from the first node of the xml file
var slideTimer:Number=Number(img_xml.firstChild.firstChild.attributes.time);
//loop for the number of nodes inside the xml
for (i = 1; i < img_xml.firstChild.childNodes.length; i++)
{
//populate the imgArray with the images
_root.imgArray[ i - 1] = img_xml.firstChild.childNodes[ i ].attributes.path;
}
//loads the first image inside the loader component
imgLoader.load( _root.imgArray[0] );
//"setInterval" to call the function "nextImage" every 2
//secs if slideTimer is equal to 2000 milliseconds
setInterval(nextImage, slideTimer);
}
//the current image is 0, the first.
var curImage = 0;
_root.onEnterFrame = function()
{
//if the image isn't 100% loaded
if (imgLoader.percentLoaded != 100)
{
//Bar is visible
Bar._visible = true
//and scale it by the percentage loaded
Bar._xscale = imgLoader.percentLoaded
} else {
//if the image is completely loaded
//the Bar needs to be invisible.
Bar._visible = false
}
}
//called by "setInterval"
function nextImage()
{
//increases current image by 1.
_root.curImage++
//if curImage is the last image then
//go back to the first one
if (_root.curImage == _root.imgArray.length)
{
//first image
_root.curImage = 0;
}
//loads the new image using the path in the imgArray.
_root.imgLoader.load( _root.imgArray[curImage]);
}
Code Explanation
- Code: Select all
var img_xml = new XML();
img_xml.ignoreWhite = true;
img_xml.load("images.xml");
- Code: Select all
var imgArray = new Array();
- Code: Select all
img_xml.onLoad = function()
{
var slideTimer = Number(img_xml.firstChild.firstChild.attributes.time);
We need this to be a number because we will use it on our setInterval().
- Code: Select all
for (i = 1; i < img_xml.firstChild.childNodes.length; i++)
{
_root.imgArray[i - 1] = img_xml.firstChild.childNodes[ i ].attributes.path;
}
- Code: Select all
imgLoader.load( _root.imgArray[0] );
- Code: Select all
setInterval(nextImage, slideTimer);
- Code: Select all
setInterval(function name, time in milliseconds);
}
var curImage = 0;
- Code: Select all
_root.onEnterFrame = function()
{
if (imgLoader.percentLoaded != 100)
{
Bar._visible = true
Bar._xscale = imgLoader.percentLoaded
- Code: Select all
} else {
Bar._visible = false
}
}
- Code: Select all
function nextImage()
{
_root.curImage++;
- Code: Select all
if (_root.curImage == _root.imgArray.length)
{
_root.curImage = 0;
}
- Code: Select all
_root.imgLoader.load( _root.imgArray[curImage] );
}

Twitter