![]() |
This is an example of a Flash movie that will parse an XML file, and display the data in a slideshow. There are a number of elements, including blank movieclips, dynamic text, and buttons. The movieclips display images loaded from the XML file. The dynamic text displays text, obviously. The buttons will be assigned URLs to navigate, which is also in the XML. Below is some of the actionscript that a Flash programmer will use. Notice that the code is almost identical to PHP. |
// declare vars
iPosition = 0; // this is kept as a local script var, but global to functions
delay = 8000; // used by the slideshow
function loadXML(loaded) {
// if the xml file exists, and is loaded, parse it into arrays
if (loaded) {
xmlNode = this.firstChild;
poster = [];
movie_title = [];
movie_link = [];
summary = [];
stars = [];
mpaa = [];
runningtime = [];
ranking = [];
ranking_img = [];
review_num = [];
trailer = [];
total = xmlNode.childNodes.length;
for (i = 0; i < total; i++) {
movie_title[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
poster[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
movie_link[i] = xmlNode.childNodes[i].childNodes[2].firstChild.nodeValue;
summary[i] = xmlNode.childNodes[i].childNodes[3].firstChild.nodeValue;
stars[i] = xmlNode.childNodes[i].childNodes[4].firstChild.nodeValue;
mpaa[i] = xmlNode.childNodes[i].childNodes[5].firstChild.nodeValue;
runningtime[i] = xmlNode.childNodes[i].childNodes[6].firstChild.nodeValue;
ranking[i] = xmlNode.childNodes[i].childNodes[7].firstChild.nodeValue;
ranking_img[i] = xmlNode.childNodes[i].childNodes[8].firstChild.nodeValue;
review_num[i] = xmlNode.childNodes[i].childNodes[9].firstChild.nodeValue;
trailer[i] = xmlNode.childNodes[i].childNodes[10].firstChild.nodeValue;
}
firstImage();
} else {
content = "file not loaded!";
}
}
//*********************************************************************************
// Let's make an XML object
xmlData = new XML();
xmlData.ignoreWhite = true; // deals with messy whitespace in xml file
xmlData.onLoad = loadXML;
xmlData.load("RSS/top10_new_in_theaters.xml");
//xmlData.load("flash/images.xml");
// Let's make a listener for keyboard
listen = new Object();
listen.onKeyDown = function() {
if (Key.getCode() == Key.LEFT) {
prevImage();
} else if (Key.getCode() == Key.RIGHT) {
nextImage();
}
}; // listen.onKeyDown
// connect listener to the keys
Key.addListener(listen);
previous_btn.onRelease = function() {
prevImage();
};
next_btn.onRelease = function() {
nextImage();
};
/*
Need a continuous loop that will shift one image to another, once the new one
loads. This will cause the fade to start once we have the new file loaded, and
fade it in slowly over 10 frames.
*/
this.onEnterFrame = function() {
filesize = picture.getBytesTotal();
loaded = picture.getBytesLoaded();
//filesize_score = movie_score.getBytesTotal();
//loaded_score = movie_score.getBytesLoaded();
//preloader._visible = true; // got rid of the preloader object, not needed
if (loaded != filesize) {
//preloader.preload_bar._xscale = 100*loaded/filesize;
} else {
//preloader._visible = false;
if (picture._alpha<100) {
picture._alpha += 10;
img_score._alpha += 10;
}
}
};
//***********************************************************************************
function nextImage() {
if (iPosition < (total - 1)) {
iPosition++;
if (loaded == filesize) {
picture._alpha = 0;
picture.loadMovie(poster[iPosition], 1);
img_score._alpha = 0;
img_score.loadMovie(ranking_img[iPosition], 1);
//title_txt.text = movie_title[iPosition];
picture_num();
slideshow();
}
} else {
iPosition = 0;
firstImage();
}
}
//***********************************************************************************
function prevImage() {
if (iPosition > 0) {
iPosition--;
} else {
iPosition = total - 1;
}
picture._alpha = 0;
picture.loadMovie(poster[iPosition], 1);
img_score._alpha = 0;
img_score.loadMovie(ranking_img[iPosition], 1);
picture_num();
}
//***********************************************************************************
function firstImage() {
if (loaded == filesize) {
picture._alpha = 0;
picture.loadMovie(poster[0], 1);
img_score._alpha = 0;
img_score.loadMovie(ranking_img[0], 1);
//title_txt.text = movie_title[0];
//score_txt.text = ranking[0];
picture_num();
slideshow();
}
}
//***********************************************************************************
function picture_num() {
current_pos = iPosition+1;
pos_txt.text = current_pos+" of "+total;
ranking_txt.text = "#"+current_pos;
title_txt.text = movie_title[iPosition];
score_txt.text = ranking[iPosition]+" out of "+review_num[iPosition]+" reviews";
summary_txt.text = summary[iPosition];
stars_txt.text = stars[iPosition];
mpaa_txt.text = "Rated "+mpaa[iPosition];
}
//***********************************************************************************
function slideshow() {
clearInterval(myInterval); // reset this or you can cause a timing click pattern
myInterval = setInterval(pause_slideshow, delay);
function pause_slideshow() {
clearInterval(myInterval);
if (iPosition == (total-1)) {
iPosition = 0;
firstImage();
} else {
nextImage();
}
}
}