-
loading data from text file into an array
Hello All,
I'm trying to load data(numbers) into an array, all ok except one thing in the following code:
for (var i = 0; i<=4; i++) {
var temp:Number = this["a"+i];
arrayData.push(temp);
}
The problem occurs when I replace (4) with variable (n) the programme gets into an endless loop.
The value of (n) comes from the loaded text file.
How can I solve this issue, thanks!
-
Prid - Outing
Hi,
Looks like you forgot to post the File Loading script You should note that it takes some time for the file to be actually loaded into Flash, and I'm talking about seconds here, like 1-2 seconds, but still, that's enough delay to not make some chunk of code work. The most likely reason that you're code is not working, is because the variable n pulled from the text file, isn't actually loaded yet once you run the code, and it ends up being undefined, thus causing en endless loop to run. You need to run the for loop ONCE the file and its content has been loaded into Flash, so that you're sure your code will work 100% of the time. To achieve this, you can use a nice AS2 class called LoadVars. This class includes an event for when the file has been loaded, and you can run your for loop in there. Here's a piece of sample code:
PHP Code:
var loadFile:LoadVars = new LoadVars(); loadFile.onData = function(data) { var n = Number(data); // assuming the file contains the value for n for (var i = 0; i<=n; i++) { var temp:Number = this["a"+i]; arrayData.push(temp); } } loadFile.load("textfile.txt");
This assumes that your textfile only contains the numeric value of n, which means it should not contain anything else, otherwise you have to solve this in a different manner. Also, the variable data inside the onData event, represents all of the data extracted from your text file.
Hope this helps
I am back, guys ... and finally 18 :P
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
-
Hello,
Thank you for your help. The code was very useful.
While I was surfing internet looking for more info about the same issue, I found the following:
onClipEvent (load) {
// create a new loadVars object
myVars = new LoadVars();
// define what has to happen once the textfile has been loaded
myVars.onLoad = function(success) {
// if the file was successfully loaded
if (success) {
myFlashArray = this.myimg.split(",");
// just a trace to check; skip in final code
trace(myFlashArray);
// skip the else partprobably in final code; or replace with something
else
} else {
trace("file could not be loaded");
}
};
// load the textfile
myVars.load("info.txt");
}
Source: http://www.justskins.com/forums/load...al-116765.html
pretty good thread!
-
Prid - Outing
Hi,
I'm glad to hear that my code was useful And that thread you posted is indeed a pretty good thread -- the way you use LoadVars in that thread is actually how LoadVars is meant to be used -- in my code, I just loaded the whole file into Flash
Regards,
Nig 13
I am back, guys ... and finally 18 :P
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
-
Hello All,
This is another good thread, worth visiting & Testing the codes, I still didn't test them; "Load variables into array" Using three diffrent methods, find it on the following link:
http://www.bokelberg.de/actionscript/28.html
Best regards!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|