Whoa there. That's a lot. Why don't I explain it my way and then you can judge if you have more questions.
First, lets forget the testMe() function. That's only for testing purposes. It has nothing to do with understanding what's going on.
So, we're left with this:
Code:
var rawdata:Array = []; // stores all the raw data
var images:Array = []; // stores symbol image file names
var urls:Array = []; // stores associated website URIs
var lv:LoadVars = new LoadVars();
lv.onData = function(src:String) {
rawdata = src.split("\r\n"); // Seperates the raw data based on line breaks. Output image.jpg|http://www.website.co,
for(i=0;i<rawdata.length-1;i++) { // Loops through all of the raw data
info = rawdata[i].split("|"); // Seperates each array element by a delimiter (vertical bar). Resulting array: info[rawdata[i]] = (info[0], info[1]) or (image, uri)
images.push(info[0]); // "Pushes" the 0th value (the image path) to the images array for later access
urls.push(info[1]); // "Pushes" the 1st value to the urls array for later access
}
}
lv.load("logos.txt");
1. The Arrays
At the top of the AS, we set three variables which represent three arrays. An array is a simple way to hold multiple pieces of data and still have one reference point.
Here we have three arrays set: rawdata, images, and urls
The rawdata array contains the full string that we parse from text file (image.jpg|http://www.website.com). The images array contains only the image portion or the part before the delimiter (image.jpg). The urls array contains the values that come after the delimiter.
How we separate what's before and after the delimiter is coming up.
2. The loadVars
I think you get this part. It's what you've been doing all along. For the benefit of a full post, loadVars is the method used to "load" the "vars" or variables from an external source.
3. The onData event
The onData event fires when all of the data from the external source has been brought into Flash and is ready to be parsed.
You ask about an alternate syntax:
Code:
lv.onData = function(thetext:String)
lv and thetext are both user-set variables. They are not explicit AS syntax. So this:
Code:
popsicles.onData = function(bananas:String)
will work just as well (but tastier) than any other syntax for those portions.
4. Split the Linebreaks
When the onData event fires, Flash has the external data in the following format:
Code:
image.jpg1|www.website1.com
image.jpg2|www.website2.com
image.jpg3|www.website3.com
It's exactly as what's in the text file. We need to break this down into manageable bits so that we can work the rest of the application properly. First, let's get rid of the line breaks.
Code:
rawdata = src.split("\r\n");
This handy-dandy bit of code says take the source data (src, again this can be anything) and split it wherever you encounter \r\n (signify line breaks) and assign it to the variable array of rawdata. At the end of that method, we're left with data that looks like this:
Code:
rawdata = array("image.jpg1|www.website1.com", "image.jpg2|www.website2.com", "image.jpg3|www.website3.com");
Each line of our external .txt file is now one element in an array. We can access a specific array with using it's index. For example:
rawdata[0] would equal image.jpg1|www.website1.com
rawdata[1] would equal image.jpg2|www.website2.com
and so on...
Notice that the count starts at zero. This is important.
5. Split based on delimiter
Now that we have the line breaks out, we'll need to separate the raw data that we have into the two parts we need -- the images and urls. To do this, we use a simple for loop:
Code:
for(i=0;i<rawdata.length-1;i++) { // Loops through all of the raw data
info = rawdata[i].split("|"); // Seperates each array element by a delimiter (vertical bar). Resulting array: info[rawdata[i]] = (info[0], info[1]) or (image, uri)
images.push(info[0]); // "Pushes" the 0th value (the image path) to the images array for later access
urls.push(info[1]); // "Pushes" the 1st value to the urls array for later access
}
The for loop has the following conditions: it starts at 0 (because remember, arrays start at 0) and it's going to perform as many loops as there are elements in the raw data array minus 1. Again, because the array count starts at 0 but the loop counter starts at 1, we need to account for this and subtract one from the rawdata.length value.
So let's split it:
Code:
info = rawdata[i].split("|");
By now you should be familiar with the split. This code is saying split the rawdata element where we encounter the "|" character. So it takes image.jpg|http://www.website.com and splits it into image.jpg and http://www.website.com.
Remember that since this is in a for loop, we're only dealing with one element at a time. So, it performs this action on the first line of the text file, then on the second line, the on the third line. It's not all at the same time.
6. Array Push
As I explained earlier, we can access specific array values by using an index. When the first line is split based on the delimiter, the result is this:
Code:
info = array("image1.jpg", "http://www.website1.com");
So, to access the image we would say info[0] and to access the website, we would say info[1].
Ok, we have the values, that should be it, right?
Not quite... Since we're doing this in a loop, each loop wipes the info array clean and inserts a new value.
So if at the end of everything you were to test for the values of info[0] and info[1] were it would show you the value of the last line in the text file. Why? Because it has looped through them all and the last value is just what's left.
So...to fix for this, what we need to do is maintain a separate array to hold these values. We already set these arrays up in the first three lines.
So, we use the array.push() method. array.push() bascially takes whatever value you feed to the function and adds it to the end of the array in question.
Code:
images.push(info[0]);
urls.push(info[1]);
The first line adds the image names to the images array. The second adds the url paths to the urls array.
That was long...sorry if it's confusing but it seemed like you were getting confused with the traces and everything so I thought I'd go over it again.
Bookmarks