Home | Flash AS3 Tutorials | Flash Animation Tutorials | Store

Flash ActionScript Tutorial: Load External Text File with Variables

This Flash ActionScript tutorial shows how to load an external text file with variables. The external text file is actually a mini text database, with variables and values associated with the variable.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

In the previous two Flash ActionScript tutorials, this is relatively easy to load an external text file to a text field of the Flash Movie. What if we wish to load the contents of the external text file to some specified text fields. This tutorial shows how to do that.

The Flash Movie of this tutorial is shown as above.

Flash ActionScript Codes:

//Create a URLLoader object with the name myLoader
var myLoader:URLLoader = new URLLoader();

 

//specify dataFormat property of the URLLoader to be “VARIABLES”
//This ensure that the variables loaded into Flash with the same variable names
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

 

//Load the text file by using URLRequest
//Can be local file or URL of webpage
//Content of the text file:
/*
Candidate=Alex
&Post=Production Engineer
&Comment=Recommend Hire
*/

 

// URL of the external text file content
var myRequest:URLRequest=new URLRequest("application.txt");

 

// Load the external image into the Loader
myLoader.load(myRequest);

 

// Note: The above two steps can be combined into a single line
// myLoader.load(new URLRequest("application.txt"));

 

//Listen when the loading of text file COMPLETE
//Call the loadComplete function when the loading COMPLETE
myLoader.addEventListener(Event.COMPLETE, loadComplete);

 

function loadComplete(evt:Event) {

//Display load completed in Message Box
output_txt.text = "The text file was loaded successfully!";

 

//Display the value with variable name "Candidate"
//HTML tag is accepted
candidate_txt.htmlText = "<b>" + evt.target.data.Candidate + "</b>";

 

//Display the value with variable name "Post"
post_txt.text = evt.target.data.Post;

 

//Display the value with variable name "Comment"
comment_txt.text = evt.target.data.Comment;

 

//Remove Listener that no longer in use
myLoader.removeEventListener(Event.COMPLETE, loadComplete);

}

 

Download Flash Source File:

Flash Source File load-text-file-3.fla

Remarks:

The above Flash ActionScript codes will display the content of the external text file when the loading process is completed. However sometimes errors may happen, for example, you change the file names of the external text file but forget to update the codes. Therefore this is always better to include error checking in your codes. The next Flash Actioncript will show how to do that.