Load variables into array
There are three ways to load variables into an array.
The first one is to provide the values in a comma
separated list and use string.split.
The second one uses numbered variablenames, the count
of variables and evil eval.
And the third uses numbers as variablenames and a
movieclip as array.
Here we go:
Version 1 (split):
Our textfile looks like this:
mylist=apple,cherry,lemon,banana&loaded=1
frame 1:
//init variable loaded
loaded=""
_level0.loadVariables("myVar.txt","");
frame 2:
// nothing
frame 3:
//loop while loaded is not set
if(loaded eq "") gotoAndPlay(2)
frame 4:
//split the text at the commas
myArray = mylist.split(",");
Version 2 (numbered vars):
Our textfile looks like this:
v1=apple&v2=cherry&v3=lemon&v4=banana&count=4&loaded=1
frame 1:
//init variable loaded
loaded=""
_level0.loadVariables("myVar.txt","");
frame 2:
// nothing
frame 3:
//loop while loaded is not set
if(loaded eq "") gotoAndPlay(2)
frame 4:
//assign the vars to array
myArray = new Array();
for(i=1; i<= count; i++){
myArray[i] = eval("v" add i);
}
Version 2 (numbers as vars, loading to mc ):
Create a moviclip and give it the instancename "datamc"
Our textfile looks like this:
1=apple&2=cherry&3=lemon&4=banana&count=4&loaded=1
frame 1:
//init variable loaded
datamc.loaded=""
//load variables to mc
datamc.loadVariables("myVar.txt","");
frame 2:
// nothing
frame 3:
//loop while loaded is not set
if(datamc.loaded eq "") gotoAndPlay(2)
frame 4:
//now we can access the vars like that
for(i=0; i<=count; i++){
trace(datamc[i])
}
- Date: 11/09/2001
- Nick Date: 29/07/2003
Hello:
Purpose:
I Have 12 flash movies which have been named in the myVar.text file as: v1=bacardi01.swf&v2=bacardi02.swf&v3=bacardi03.swf (and so-on up to 12).
Am using Flash4 and am fairly new to actionscripting and have used Version 2 (numbered vars) of the 'Load Variable into Array', but have no results.
In Frame 4 I used the "Set Variable Action"
where variable is: "myArray"
value is: new Array(); for (i=1; i<= count; i++) {myArray [i] = eval ("v" add i); }"
Please suggest where I may have gone wrong.... :-(
Many Thanks in Advance,
Nick
Add comment
Home