PDA

View Full Version : populating array from textfile... and using it!



radioxromance
June 13th, 2004, 05:12 PM
Okay so I am new to using txt files, and not too great with arrays, but I think I should have this right. But when I try to trace the array, I get undefined!
I found this on another thread, and changed it accordingly.


_root.textArray = new Array();
_root.text_lv = new LoadVars();
_root.text_lv.onLoad = function(success) {
if (success) {
_root.textArray = this.texts.split(",");
}
};
_root.text_lv.load("links.txt");
trace(_root.textArray[0]);

I changed a few thigns such as adding _root cause I thought maybe for some reason it was scope, but that didn't fix it. But if I trace it inside the success if, I get the proper trace! WHY?

claudio
June 13th, 2004, 06:11 PM
The tracing happens before the textfile content can be assigned to the array. To work around that, use the onLoad handler of your LoadVars object. Everything that goes inside the onLoad happens after the data has been loaded.


_root.textArray = new Array();
_root.text_lv = new LoadVars();
_root.text_lv.onLoad = function(success) {
if (success) {
_root.textArray = this.texts.split(",");
trace(_root.textArray[0]);
}
};
_root.text_lv.load("links.txt");