The loadVariables MethodThe loadVariables() method was introduced back in Flash 4, and it has the capability to load data from external files (like the sample.txt file we are going to be using) as well as from middleware such as PHP and ColdFusion, which are discussed in later chapters. It loads the data directly into a movie clip object that must be specified when called. Its general layout looks like this: loadVariables(URL,target,method); Here is what each parameter means:
Now that you see what it looks like, let's start using it. In the next example, we are going to call the text file we have previously created from within Flash using the loadVariables() method. We will store the data in the _root timeline by using the keyword this.
We call the text file we have already created that resides in the same directory as the Flash movie, and we place the variables on the main timeline using this. We are not interfacing with middleware, so the method parameter is left out. Now test the movie. You'll see that it appears that nothing has happened, but on the contrarywhile still in the test movie screen, select Debug, List Variables, and you will now see the variables that have been brought in displayed in the Output panel, as shown in Figure 20.3. Figure 20.3. The Output panel lists the variables that are available in the movie.Now that you can see that the variables are being brought in and parsed correctly, we are going to use them. But before we can use them, we have to know when they have loaded in completely. Even though they appear to have loaded in instantly, they did not, and on the web, it is even more important to know when the variables have completed loading because over some dial-up connections, it may take a while. Fortunately, there is already an event built in to Flash to let us know when data has been fully received in a movie clip: the onData event. The onData EventThe onData event is an event that is triggered whenever the associated movie clip receives data using the loadVariables() or the loadVariablesNum() method. When used in conjunction with the _root timeline, it will receive this event automatically. We can set a function to this event the same way we have been setting functions to events: movieClip.onData = function(){ //do something } The movie clip can be any movie clip created either manually or with ActionScript including the _root timeline. Now that you see what the event is supposed to do, let's put it into practice with this next example.
Notice that this time, we create a movie clip object in our code to be the data holder for the data coming in. Now test the movie, and when the data is received, the Output panel should appear similar to Figure 20.4, with all of the information from the text file. Figure 20.4. The onData event is triggered when the movie clip receives data.That was awesome! We loaded some basic information into Flash and then had an event let us know when the data had been fully loaded. But what if we want to have multiple pieces of data in a single name/value pair? You cannot use arrays in the text file; they are not supported to be loaded in that way. There is a workaround for this bottleneck. Even though you cannot store data in arrays in a text file to be loaded in, you can separate data in a single name/value pair with unused characters, such as "~" or "$", and then separate them and store them in an array after they have been received by Flash. In this example, you will need another text file named sample2.txt stored in the same directory as the other text file with this data in it: &names=David~Ben~Doug~Paul~Lesley~Todd~Missy& Notice that we used the tilde (~) character to separate each name in the single name/value pair. Now that we have the text file we need, we can do another example. This example will have several stop points so that we can see the progression all the way until we fill a ComboBox component with the names.
This code should look familiar; we created the movie clip to handle the data coming in. Then we called the text file with the data within it, and placed the data in the holder_mc movie clip. If you test the movie, when data is received, the variable that is brought in, names, is traced, showing all the names in the text file separated by tilde (~) characters. Now that we know the data is coming in, we can go to the next step, which will separate the names variable and store the information in an array. Then we will trace the array. To do this, we will use the split() method.
The preceding line of code takes the array that was filled with the data we brought in and sets it as the dataProvider for the ComboBox component. Now test the movie, and the end result should look like Figure 20.5, with all the names inside the ComboBox. Now you can really begin to see the benefits of dynamic data. All you have to do is add, change, or remove names from the text file sample2.txt, and the movie, when run, will display the new information without any more work needing to be done in Flash. Figure 20.5. You can use dynamic content in conjunction with interface pieces quickly and easily.There is another way to load content into a movie clip from an external source: the loadVariablesNum() method. |