I would like to load an Array from a file into flash. I need it because I need to use a random element from that array at various times and have a variable value as the the index for which element I want to use.
Is is possible to get an array from a file? Or am I just dreaming?
Well you can’t dynamically load an array itself, but you can parse it into an array.
An example would be something like this…
TextFile (named array.txt for this example, but can be whatever) has…
myArray=1,2,3,4,5,6,7,8,9,0
On a frame in flash… [AS]myLV = new LoadVars();
myLV.onLoad = function(success){
if (success){
numArray = this.myArray.split(“,”);
} else {
trace(“file failed to load”);
}
myLV.load(“array.txt”);[/AS]
This loads the array.txt file and when it loads it parses the myArray variable into an Array by splitting it at the “,” symbol. So this will essentially give you an array that looks like this… [AS]numArray = [1,2,3,4,5,6,7,8,9,0][/AS]
You can use any symbol to split at BTW. I know a lot of people use | and some use stuff like :: to seperate their content. Of course you set that in the this.myArray.split() line
ok, well, I was using the loadVariables() function before, and having no problem. I tried to do it your way, and it seems like nothing much is happening. Per your example, do I have to refer to variables after they are loaded as
myLV.numArray ?
Otherwise, I can’t figure out what’s wrong, I didn’t change any other code, just added the onLoad and used a LoadVar to call the load() function instead of doing LoadVariables().
Am I right that in this case I can’t use numArray anywhere else than inside the onLoad function? Do I have to write everything where I need the numArray (the whole program) inside onLoad function or is there some other way?
Also in this case why do I get “Hi” before “Hello”?
Doesn’t myLV.load call onLoad wich outputs Hello and Hi comes after that.
Am I right that in this case I can’t use numArray anywhere else than inside the onLoad function?
Now you can use it AFTER the onLoad function, but since it is created when the file loads, you can’t use it before that.
Also in this case why do I get “Hi” before “Hello”?
All code in the frame is interpreted before your file loads, so “Hi” will be interpreted and displayed before “Hello” since “Hello” waits until the file is loaded.
*Originally posted by lostinbeta *
**Now you can use it AFTER the onLoad function, but since it is created when the file loads, you can’t use it before that.
**
So, is the solution to use numArray not until the next frame? Sorry, I’m not yet quite familar with the structure of Flash. :hangover:
Well if you are able to do what you have to do in the onLoad, I recommend doing it then. You can call it in the next frame, but only if you play that frame after the onLoad is complete.
Depending on what modem the user has and the speed of your server, the loading time of the file will be different so running your code in the onLoad is really your best option.