Flash AS3 Tutorials Center | Interesting Flash Animation Tutorials | Valuing Flash Animation Tutorials | Flash ActionScript and Animation Home

AS3 Beginner Tutorials | AS3 Basic Lessons | AS3 Valuing Courses | AS3 Components Tutorials | AS3 and PHP Interaction Tutorials
AS3 Practical Tutorials | AS3 Animation Techniques | AS3 Transition Effects Tutorials | AS3 Download Upload Files | AS3 Particle Systems
Communication Between Flash Movies with AS3 | AS3 and JavaScript interaction | AS3 Matrix Transformation | AS3 Physics Simulation Tutorials

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.