Part 2: Loading External Data From A Textfile
Textfiles are the first form of pulling in dynamic content to Flash that I will talk about. You can use the simple loadVariables or loadVariablesNum command to pull data from a textfile directly into Flash, but before we get into the loading, we must get in to the textfile formatting.
Formatting Your TextfilesThere are a few reserved characters in URL encoding that you cannot use....the ampersand (&), the plus sign (+), and the percent sign (%). As stated above, these are all used by URL encoding for special purposes. The use of these characters in your variables can confuse and cause problems. The ampersand would be "%26", the plus sign would be "%2B", and the percent sign would be "%25". You of course cannot use the equal sign (=) in your variable names either.
Carriage returns in your textfiles may look like the correct spacing you want, but once pulled into flash your spaces will be enlarged. To avoid this, you should use "%OD" for your returns or change the line spacing of your textbox in Flash.
To make it easy on yourself there are certain techniques that you might want to adopt. Instead of writing a query string in your texfile like this.....
name=Ian+Miller&website=http://mw9.net&age=18&loaction=california
You see how that could be hard to read if you wanted to go back and edit it a little? Think about a textfile with 20 contacts. Try writing it out like this in your textfile.....
&name=Ian Miller&
&website=http://mw9.net&
&age=18&
&location=california&
See how much easier it is to read. The ampersands tell flash where the variables are so the returns are ignored. It is far easier to read and go back to edit. Try using this method if you enter data into textfiles for Flash.
EXAMPLE-Make a dynamic textbox on your main timeline.
-Go to the properties window and make that variable "Show".
-Make a button on your main timline.
-Put this code under it (right click the button > actions).
on (release) {
loadVariables("test.txt?"+random(999), _parent);
}
-Make a textfile name "test"
-Put this in it....
&Show=testing, it works!!!&
Here is an example of it.....
swfHere is the fla if you would like to take a look.....
flaFormatting Arrays in TextfilesForamtting an array in a textfile is actually quite simple, but requires a little help on the Flash side. If actionscript you would create an array like this....
ContactNames=new Array("Ian","Jen","Jeff","John","Linda");
Now to do this in your texfile you would use this code....
&ContactNames=Ian,Jen,Jeff,John,Linda&
Flash would pull this in as a single variable with the actual value of "Ian,Jen,Jeff,John,Linda". This is where the Flash help comes in. You will need to preform the split() action on it.
stringName.split("Character", [limit])
Character-The character that you wish to split your array by.
Limit- The limit of the number of items that you wish to be placed in your array. This parameter is optional and for the most part you will not use it.
Now to apply this to our example, you would use this code.
ContactNames.split(",");
It is really simple. The textfile variable in conjunction with the split command does the same thing as the actionscript command new Array at the begginning of this section. That concludes the formatting of textfiles section. Use these guidlines and you will save alot of time and trouble.