Alright, so let's get some images in.
First, let's make a small change to the XML to make it a bit more flexible and easier to update. It seems like all of your images are in the same directory. If this is the case, then follow the steps below.
I've added an attribute to the <gallery> node called path. This will contain the directory path to the images. My images are in the images/ directory, so I have something like this:
Code:
<gallery path="images/">
<page>
<left>1.jpg</left>
<center>2.jpg</center>
<right>3.jpg</right>
</page>
...
Now, let's add a variable in Flash to store this information.
First, initiate the variable:
Code:
// Variables
var total:Number = new Number();
var path:String = new String();
Then, set the value within the xml.onLoad() loop:
Code:
// Load XML
xml.onLoad = function(success) {
if(success) {
total = xml.firstChild.childNodes.length // Set total number of pages
path = xml.firstChild.attributes.path;
loadImages();
}
else {
trace("Error: XML Didn't Load");
}
}
Next, we'll need to set up an array to hold the names of the three "picture frames". This will allow us to use easier coding techniques to refer to each of these movieclips. We could refer to each by name directly, but that would require repetitive code. If I want all three to behave the same way, it's much easier to assign the property to each at the same time.
So, lets create the array. We'll call it positions.
Code:
// Variables
var total:Number = new Number();
var path:String = new String();
var positions:Array = new Array(left, center, right);
Now, we get to the good stuff. Let's create the loadImages() function. In order to reuse the function, we'll add pass a paramater -- the page number. This will allow us to target particular nodes within the XML.
Here is the code. I'll explain it below.
Code:
// Load Images
function loadImages(page) {
pageNo.text = "PAGE " + (page+1) + " OF " + total;
for(i=0;i<3;i++) {
var imagePath:String = path + xml.firstChild.childNodes[page].childNodes[i].childNodes
if(imagePath!="") {
positions[i].loadMovie(imagePath);
}
}
}
First, note that I'm passing in a parameter (page) into the function. Second, one thing to always keep in mind is that Flash counts starting with 0. So, the first element in an array is at index 0, not 1. The first node in the XML is at index 0, not 1.
The very first thing I do in the function is display the page number. The parameter (page) isn't technically the page number. At least, not in human readable format. It's the index of the node. So, to make it human readable, we need to add one.
Next, we have a for loop. Since there are three positions (and three corresponding nodes in the XML), I'm looping through the XML three times. If you need more info on for loops, let me know. So bascially, I'm reading the value of each child node of the <page> node within the loop. The first time, it reads the first node (<left>), the second time it reads <center> and so on...
I then take whatever value that is and set it equal to a variable -- imagePath. Note that I'm concatenating the path variable from earlier to this value so we get the full path to the image. If the variable isn't blank, I load the image into the appropriate movieclip.
This is where the array comes in. The element in the array and the node of the XML have the same index. Node 0 of the XML (<left>) can then be linked with the movieclip left (which is at index 0 in the array) through the same "variable" (i in this case).
Hopefully that's not too confusing. If you have any questions, please ask. Putting it all together, your AS should not look something like this:
Code:
// Initiate XML
var xml:XML = new XML();
xml.ignoreWhite = true;
// Variables
var total:Number = new Number();
var path:String = new String();
var positions:Array = new Array(left, center, right);
// Load XML
xml.onLoad = function(success) {
if(success) {
total = xml.firstChild.childNodes.length // Set total number of pages
path = xml.firstChild.attributes.path;
loadImages(0);
}
else {
trace("Error: XML Didn't Load");
}
}
// Load Images
function loadImages(page) {
pageNo.text = "PAGE " + (page+1) + " OF " + total;
for(i=0;i<3;i++) {
var imagePath:String = path + xml.firstChild.childNodes[page].childNodes[i].childNodes
if(imagePath!="") {
positions[i].loadMovie(imagePath);
}
}
}
xml.load("test.xml");
You should now be loading the images from the first set of images in the XML.
Next, we'll add the navigation (next and previous buttons).
Bookmarks