Hello AS experts. I am trying to create an array where each element is a different text string that is loaded from separate external text files. What I have below almost works -- I can load in and trace each of the different strings at each step of the loop, but I cannot get them to be plugged into each element of an array. See below for the output when running this script. I know the problem lies at the line where I wrote: Doesn't work!. Does anybody know what I need to have here to make this work? function createTitleArray() { var i:Number = 0; var n:Number = Number(slideTotalLV.slideTotal); //from another LoadVars and is equal to "7" (an integer) var slideTitleLV:LoadVars = new LoadVars(); var imgTitle:Array = new Array(n); while (i < n) { slideTitleLV.load("../var/info_" + i + ".txt"); //opens files named "info_0.txt", "info_1.txt", ... , "info_6.txt" slideTitleLV.onLoad = function(success:Boolean) { if (success) { trace(slideTitleLV.title + " [from loop]"); //in each of the *.txt files there is a variable "title" that is equal to some text. There are traced correctly } } imgTitle = [String(slideTitleLV.title)]; //Doesn't work! Here is where I am trying to place contents of each "title" variable in an element of an array. i += 1; } trace("titleArray = " + imgTitle + " [end]"); //"undefined" gets traced as each of the seven elements in the array. } OUTPUT titleArray = undefined,undefined,undefined,undefined,undefined,undefined,undefined [end] First Test Title [from loop] // these seven rows were retrieved from the "title" variable from the "info_0.txt", "info_1.txt", ... , "info_6.txt" external file sequences. Second Test Title [from loop] Third Test Title [from loop] Fourth Test Title [from loop] Fifth Test Title [from loop] Sixth Test Title [from loop] Seventh Test Title [from loop] What I want is for these last seven lines to be the elements in the array that has only "undefined" elements. Please help... Regards, -john
imgTitle[ i ] = [String(slideTitleLV.title)]; This line is wrong. You added a pair of brackets that aren't needed. Just use:
NSurveyor, I had already tried: imgTitle[ i ] = String(slideTitleLV.title); (without square brackets and that didn't work either, so I threw in the [ ] before posting). Any other suggestions? I am going crazy here since it seems that imgTitle[ i ] = String(slideTitleLV.title); should work. Thanks for your first reply, -john
Look, I don't have 2004 but I think the problem is a lot more simple than that... The files are loaded when you are trying to associate the element to the array... That is why your loop returns the output as "undefined" for all items... but then when the successful loading of the loadVars, is able to show all the correct values with the "[from loop]". so now for a solution... don't do it like that... obviously what you are after is to generate an array with information from the loadVars(success)... so do just that... slideTitleLV.onLoad = function(success:Boolean) { if (success) { imgTitle.push(slideTitleLV.title) // and whatever else... } } then outside the loop that brought in your variables... trace("titleArray = " + imgTitle + " [end]"); one other thing... i += 1; can also be written as i++; and I am lazy and that is less keystrokes. I hope that helps, cheers,
Mandingo, Thanks for your imput, the sharing of your knowledge is very much appreciated. I will try your suggestion to see if I can get it to work.
Don't see what you're looking for? Try a search.
|