Data being loaded into Flash by means of loadVariables(), loadVariablesNum(), or the LoadVars class must be in standard MIME format. It is in application /x- www-form-urlencoded format and is basically a bunch of name/value pairs.
Following is a basic example of a name/value pair in MIME format:
Unlike ActionScript, here we do not put the value of the variable name in quotes. Also, there is no space after the equal sign; this is because if there were a space, the value would include the space, making "David" become " David".
The preceding example had only one name/value pair. To separate multiple name/value pairs, the ampersand (&) is used like this:
You can, of course, put ampersands on either side of the name/value pair just to make sure there are no errors, like this:
This way, you can clearly tell where each name/value pair begins and ends.
You can also create URL-encoded strings in Flash for special characters, such as the ampersand, using the escape() function.
The escape() and unescape() functions
Because an ampersand is used to separate individual name/value pairs in URL-encoded content, they cannot be within the content itself or Flash will read it as a separator, which can cause errors. Using the escape() function, you can encode the ampersand in a URL-encoded format, as in the following code:
//create the string var myString:String = escape("title=War & Peace"); //send the encoded string to the output panel trace(myString); //output: title%3DWar%20%26%20Peace
As you can see, the string being created is URL-encoded and stored in a variable. Then the string is sent to the Output panel, where you can see that the ampersand and the spaces in the string have been replaced with special characters. To get the string back to normal, use the unescape() function, which will convert URL-encoded strings back to their original format like this:
//create the string var myString:String = escape("title=War & Peace"); //send the encoded string to the output panel trace(myString); trace(unescape(myString)); //output: title%3DWar%20%26%20Peace // title=War & Peace
Now that you have seen the basic format that the data we are going to load needs to be in, let's make a file to hold the information that we will be using in several examples throughout this chapter.
Now that you have a file you can work with, let's go over some of the methods used to bring in data from external sources, starting with the loadVariables() method.