Tutorial Instructions


In the AS2 version of load variables it was a little more confusing on how to retrieve variables from an outside resource such as a text file or a webpage. The new AS3 way is much simpler in the retrieval of variables but you have to implement a few things before we can get to the actual retrieval part.

Step 1: Create the data file

In the first part of this tutorial we are going to create a data file called data.txt where we will put the variables to be loaded into the movie. So create the file data.txt and save it in the same folder as your .fla file is saved in. We will put the following code into the text file:
var1=name&var2=number&var3=1922&var4=14567
You will notice how we created four different variables: var1, var2, var3, and var4. It is important to notice that each variable must end with an & at the end to denote when we are starting a new variable.

Step 2: Create the .fla file

What we are going to do is create 4 dynamic texts to show the variables once they are loaded into the movie. We will also create 4 labels so it kind of looks something like this:


We will name instance variables of the dynamic texts as such. Name the first one variable1, the second one variable2, the third one variable3, and the last one variable4.




Step 3: Add the code to the frame

Alright so we need to do a few things here. We need to setup a loader where we can call the file and load it in. We also have to set the parameter of the loader to variables since that is what we are retrieving. So first add the following code:
// Need to create a URL Loader called loader
var loader:URLLoader = new URLLoader();

// specify what we are loading, in this case variables
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
The first part of the code creates a URLLoader which will let us load the data from a designated url that we will add in a second. The second part designates the dataFormat which we chose to set as VARIABLES because that is what we will be retrieving. It would change, for example, if we decided to load an image.

Now we need to add an event listener to call a function after the URL is loaded. The following code shows us how to do that:
// add an event listener to do the function when done loading
loader.addEventListener(Event.COMPLETE, varsLoaded);
The event listener will let us call the function varsLoaded on the completion of the laoder loading in the URL. Now we need to go ahead and load a URL with the following code:
// actually call the loader to load the file
loader.load(new URLRequest("data.txt"));
Now you see that we called load with a new URLRequest with the link of the location of the file we created data.txt. The URL is simple just "data.txt" because we kept it in the same folder as the movie.

Now the last part of the code we need to add is the function to set the dynamic texts we created in the second part of the tutorial. The function we know is already called varsLoaded as defined by the event listener we added. So lets create the function:
// set the variables from the data.txt file
function varsLoaded (event:Event):void {
	variable1.text = loader.data.var1;
	variable2.text = loader.data.var2;
	variable3.text = loader.data.var3;
	variable4.text = loader.data.var4;
}
You will notice that the parameters is the event and the function is void because it returns no data, just sets the dynamic texts. So we set the dynamic texts with variable1.text=. We set the variable to the laoder.data.var1. We call var1, because that is what we named it in the data.txt file.

That should complete the tutorial. Your final result when testing should look like this:




Complete Source Code and Download

Here is the complete source code and a download for the source files:
// Need to create a URL Loader called loader
var loader:URLLoader = new URLLoader();

// specify what we are loading, in this case variables
loader.dataFormat = URLLoaderDataFormat.VARIABLES;

// add an event listener to do the function when done loading
loader.addEventListener(Event.COMPLETE, varsLoaded);

// actually call the loader to load the file
loader.load(new URLRequest("data.txt"));

// set the variables from the data.txt file
function varsLoaded (event:Event):void {
	variable1.text = loader.data.var1;
	variable2.text = loader.data.var2;
	variable3.text = loader.data.var3;
	variable4.text = loader.data.var4;
}


Download the Source Files


Comments


You must be logged in to post comments...