View Full Version : ActionScript Explained.
Lewwy
August 10th, 2005, 11:37 AM
Could someone please explain this code to the best of thier ability?
var x:XML = new XML();
x.ignoreWhite =true;
var urls:Array = new Array();
var captions:Array = new Array();
var whoISOn:Number;
x.onLoad = function() {
var photos:Array = this.firstChild.childNodes;
for (i=0;i<photos.legnth;i++) {
urls.push(photos[i].attributes.url);
captions.push(photos[i].attributes.captions);
}
holder.loadMovie(urls[0]);
caption.text = captions[0];
whoIsOn = 0;
}
x.load("lost.xml");
lunatic
August 10th, 2005, 11:48 AM
//create a new xml object called x
var x:XML = new XML();
//text nodes that only contain white space are discarded during the parsing
//process. Text nodes with leading or trailing white space are unaffected.
x.ignoreWhite =true;
// create new array object called urls
var urls:Array = new Array();
// create new array object called captions
var captions:Array = new Array();
// create variable to hold numbers called whoISOn
var whoISOn:Number;
// when xml object loads
x.onLoad = function() {
// create array called photos and set its value to the child node of the first
// child node in the xml file (or something like that - a look at the xml file
// should make it obvious)
var photos:Array = this.firstChild.childNodes;
// for loop runs through length of photos array
for (i=0;i<photos.legnth;i++) {
// fills the url array with url attribute from xml file
urls.push(photos[i].attributes.url);
// fills caption array with captions attribute from xml file
captions.push(photos[i].attributes.captions);
}
// load first spot in url array into an empty movie clip called holder
holder.loadMovie(urls[0]);
// load first spot in caption array into textbox
caption.text = captions[0];
// sets whoIsOn to 0
whoIsOn = 0;
}
// load xml filed called "lost.xml"
x.load("lost.xml");
hope this helps! :hr:
Lewwy
August 10th, 2005, 01:08 PM
var photos:Array = this.firstChild.childNodes;
// for loop runs through length of photos array
for (i=0;i<photos.legnth;i++) {
// fills the url array with url attribute from xml file
urls.push(photos[i].attributes.url);
// fills caption array with captions attribute from xml file
captions.push(photos[i].attributes.captions);
Thanks for the explanation. Its helped me alot. But you think you could elaborate on the selected stuff above?
In particular the 'i < 100' crap. Its all dutch to me :( .
lunatic
August 10th, 2005, 01:20 PM
lol, it's called a for loop. You set the parameters for the compiler to run through.
Let's go through each part of the for loop:
i=0
i is an arbitrary variable used only in the for loop - it gets discarded outside the function. Don't know how much you know about arrays but the first spot in an array is always the zero spot. Don't ask me why. :P So if you have an array called fruit and in it you have
fruit=['banana', 'orange', 'apple', 'kiwi']
There are four items in the array but they occupy the 0 through 3 spots. As in
fruit[0]=banana
fruit[1]=orange
fruit[2]=apple
fruit[3]=kiwi
If you still don't get arrays check out the tutorial on kirupa.com - very simple to understand. :)
Okay so anyway, that's why the for loop starts at i=0, because we want to start at the first spot in the array. If you started with i=1 the code would skip the first item in the array (in our case 'banana').
i<photos.length
Now, we want to cycle through the function a certain number of times, that is, the number of items in the array. If there are 4 items in the array we want to cycle through 4 times to make sure every item goes through the function. You could have said i<4 but what if you decide later to add more items to the array? Then you have to remember to change that 4. By grabbing and using the length of the array you never have to worry about how long the array actually is.
i++
This just keeps increasing i by one every time it goes through the function. By starting at 0 you run the function with the item in the 0 spot. At the end of the function i gets increased by one so now i=1 and it goes through the function again with whatever is in the 1 spot of the array. At the end of that function i=2 and so on. It will keep doing this until it gets to the last spot in the array.
Let me know if you have any more questions! :hr:
Lewwy
August 10th, 2005, 01:49 PM
Thanks, you pretty much just explained everything..
But it still isnt working :D.
lunatic
August 10th, 2005, 02:05 PM
Well now THAT'S a whole different story! :lol:
lostinbeta
August 10th, 2005, 02:17 PM
Ok, so we established you wanted the code explained.
And then you mention that it just isn't working.
LOL... you couldn't have mentioned that first? And given a description of how exactly it isn't working?
Did you try using the 'success' attribute of the onLoad? var x:XML = new XML();
x.ignoreWhite = true;
var urls:Array = new Array();
var captions:Array = new Array();
var whoISOn:Number;
x.onLoad = function(success) {
if (success) {
trace("LOADED!");
var photos:Array = this.firstChild.childNodes;
for (i=0; i<photos.length; i++) {
urls.push(photos[i].attributes.url);
captions.push(photos[i].attributes.captions);
}
holder.loadMovie(urls[0]);
caption.text = captions[0];
whoIsOn = 0;
} else {
trace("COULD NOT LOAD FILE");
}
};
x.load("lost.xml");
lostinbeta
August 10th, 2005, 02:19 PM
Also..
for (i=0; i<photos.legnth; i++) {
urls.push(photos[i].attributes.url);
captions.push(photos[i].attributes.captions);
}should be changed to for (i=0; i<photos.length; i++) {
urls[i] = photos[i].attributes.url;
captions[i] = photos[i].attributes.captions;
}This is because of how a for loop works, by looping through and outputting the final data in one lump sum... so push doesn't work correctly. At least I think that's why.
And you were spelling length as legnth... so that could be it too.
lunatic
August 10th, 2005, 02:21 PM
thanks lost, I didn't know that about push in for loops :)
I'm gonna bet lewwy was hoping that if he/she knew how the code worked then he/she could it figure it out but that didn't happen.
lewwy - definitely try the success thing to see if the xml file is even loading. Also, are you getting error messages or it just isn't working?
:hr:
Lewwy
August 10th, 2005, 02:39 PM
Ok, so I added the little 'LOADED' doo-hicky and it worked but I was confronted with a new error.
LOADED!
Error opening URL "file:///C|/Documents%20and%20Settings/Lewis/My%20Documents/Projects/Flash/XML%20Gallery/1.jpg"
1.jpg being my first photo in my XML file.
Then I changed the for loop problem and now im getting this error
LOADED!
Error opening URL "file:///C|/Documents%20and%20Settings/Lewis/My%20Documents/Projects/Flash/XML%20Gallery/undefined"
I have 1.jpg in a folder called 'XML Gallery' along with the .fla, .swf and lost.xml.
Need anything else?
Edit : Im a 'He' :D
lostinbeta
August 10th, 2005, 02:44 PM
Well your first method worked because it returned '1.jpg'.
So the only problems I can think of now are
A) Your path to the folder is incorrect... check the path it returns, is it accurate?
B) If so, is your image saved as Non-Progressive (it's an option for most graphics programs in the Save As panel for .jpg format). It is important that it be a non-progressive .jpg image. Otherwise it cannot load.
Other than that.... I'm lost.... and not as in that's my name.
lunatic
August 10th, 2005, 02:44 PM
Edit : Im a 'He' :D
Thought so, but didn't want to make assumptions. ;)
What did you change the for loop to?
Lewwy
August 10th, 2005, 04:25 PM
What lost suggested.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.