Well the XML we use here is so simple and you’d be more comfortable with it if you have a previous knowledge of HTML.
The thing you need to know is that you can create XML files using NOTEPAD and then saving them as .XML file you can still edit them with notepad after that. The next step is creating your XML data file.
You should always begin the XML file with this line it is called the identifier:
<?xml version="1.0" encoding="UTF-8"?>
The you open a tag for your parent entity:
<Gallery>
Then you begin to add your children entities
<image>
with their respective attributes in our case imageName and state. For state i used ‘1’ for landscape and ‘2’ for portrait.
Your final XML file should look somrthing like this:
<?xml version="1.0" encoding="UTF-8"?>
<Gallery>
<image imageName = "image01.jpg" state = "1" />
<image imageName = "image02.jpg" state = "2" />
<image imageName = "image03.jpg" state = "2" />
</Gallery>
and so on you must use this format or the XML parser in flash will not interpret the XML well.Do not use an empty line between to child entities entries.
A last note about XML if you have a large number of photos for example 100 it will be very time consuming to file the images name in the notepad i advice you to rename the images automatically using photoshop or ACDSee in a series from image001.jpg to image100.jpg then use EXCEL to provide the string name squence and you can simply copy and paste the sequence from EXCEL to an XML editor called XMLSpy.
Now you got your XML file the next step is to get the data from it.
you can use this actionscript code it is straight forward any strange command is clearly explained in the FLASH MX actionscript reference.
[AS]
stop();
XML.prototype.ignorewhite = true;
var _root.imagePointer = 0;
var ImageName = new Array();
var ImageState = new Array();
var myXML = new XML();
myXML.load("…/XMLFiles/myXML.xml");
function getFilesNames() {
// Parse XML
imageCount = tempXML.firstChild.childNodes.length;
node = tempXML.firstChild.childNodes;
for (var i = 0; i<myXML.firstChild.childNodes.length; i++) {
ImageName.push(node*.attributes.imageName);// Store the information of the imageName attribute in ImageName array
ImageState.push(node*.attributes.state);//same as above
}
return (imageCount);
}
if (myXML.loaded) {
play();
}
[/AS]
since sometimes the loading of the XML file may have some errors the last if statement is used to confirm loading the file.
when you reach the next frame you’ll have the following code
[AS]
stop();
getFilesNames(); // use the previous function to parse the file
if (ImageState[0] == 1) {
_root.mainImage._x = 204; // good position for landscape images
_root.mainImage._y = 180;
} else {
_root.mainImage._x = 304; // good position for portrait photos
_root.mainImage._y = 80;
}
// in my case i created on the root an empty movieClip to hold the images with an instance name mainImage
_root.mainImage.loadMovie("…/PhotosFolder/" + ImageName[0]);
// use this command to initially place the first image on stage
// in your case you have two buttons on stage one to cycle through the images upward (next) and one downward (previous)
// next and previous are the instances’ names of the buttons on the root
_root.next.onPress = function () {
if (_root.imagePointer == imageCount) {
_root.imagePointer = 0; // return to the first image if you are at the last one
} else {
_root.imagePointer ++; // increment pointer
}
if (ImageState[_root.imagePointer] == 1) {
_root.mainImage._x = 204;
_root.mainImage._y = 180;
}else {
_root.mainImage._x = 304;
_root.mainImage._y = 80;
}
_root.mainImage.loadMovie("…/PhotosFolder/" + ImageName[imagePointer]);
}
_root.previous.onPress = function () {
if (_root.imagePointer == 0) {
_root.imagePointer = imageCount; // return to the last image if you are at the first one
} else {
_root.imagePointer --; // decrement pointer
}
if (ImageState[_root.imagePointer] == 1) {
_root.mainImage._x = 204;
_root.mainImage._y = 180;
}else {
_root.mainImage._x = 304;
_root.mainImage._y = 80;
}
_root.mainImage.loadMovie("…/PhotosFolder/" + ImageName[imagePointer]);
}
[/AS]
That’s all hope everything is clear