The loadVariables Method


The loadVariables Method

The 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:

  • URL This is the string path to either the file holding the data or to the middleware that will send back the data. It can be either an absolute path or a relative path to the file. (The URL must reside on the same domain as the Flash file when on the web.)

  • target This is the name of the movie clip object that will receive the data.

  • method This is an optional parameter for use with middleware that tells how the data will be sent and received from the web server with either GET or POST.

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.

1.

Create a new Flash document.

2.

Save this document as sample-20-1.fla in the same directory as the text file you have already created.

3.

Open the Actions panel in the first frame of the main timeline and place these actions within it:

 //get the data from the text file loadVariables("sample.txt",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 Event

The 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.

1.

Start a new Flash document.

2.

Save this document as sample-20-2.fla in the same directory where the sample.txt file is saved.

3.

Name the layer actions, and in the first frame of that layer, place these actions:

 //create the movie clip to hold the data this.createEmptyMovieClip("holder_mc",1); //get the data from the text file loadVariables("sample.txt",holder_mc); //when the movie receives the data holder_mc.onData = function(){      trace("my name is - "+this.name);      trace("my age is - "+this.age);      trace("my location is - "+this.location); } 

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.

1.

Start a new Flash document.

2.

Save this document as sample-20-3.fla in the same directory as the text file you just created.

3.

Create another layer and name the top layer actions and the bottom layer content.

4.

In the first frame of the actions layer, open the Actions panel and place these actions in it:

 //create the movie clip to hold the data this.createEmptyMovieClip("holder_mc",1); //get the data from the text file loadVariables("sample2.txt",holder_mc); //when the movie receives the data holder_mc.onData = function(){      trace(this.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.

1.

Replace the function for the onData event with this one:

 //when the movie receives the data holder_mc.onData = function(){      //create an array to hold the data      var myNames_array:Array = new Array();      //put the data in the array      myNames_array = this.names.split("~");      trace(myNames_array); } 

Now if you test the movie, instead of the names being separated by tilde characters, they are separated by commas showing that they have indeed been placed into the array.

2.

The next step is to select the content layer and drag a ComboBox component onto the stage. Give it an instance name of myCombo_combo.

3.

Finally, replace the trace statement in the onData event with this line of code:

 myCombo_combo.dataProvider = myNames_array; 

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.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net