Flash ActionScript Tutorial: Load External Text File (1)
This tutorial series show how to load external text files with ActionScript 3 with Flash CS3. The first two tutorials will show how to load an external text file into a text field. The external text file is a simple story...
Flash Tutorial Content:
Many Flash Movies require to load external text files. The text file may be a small database or simply some information. In this Flash ActionScript tutorial, we load an external text file which is simply a story into a text field of the Flash Movie.
The first 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 propery of the URLLoader to be "TEXT"
// (This step is optional since the default dataFormat is "TEXT"
myLoader.dataFormat = URLLoaderDataFormat.TEXT;
// Create a new URLRequest object
// specifying the location or URL of the external text file.
var myRequest:URLRequest=new URLRequest("story.txt");
// Call the load( ) method to load the external file
// with the URLRequest object as the parameter.
myLoader.load(myRequest);
// Note: The above two lines of codes can be combined into one line:
// myLoader.load(new URLRequest("story.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) {
output_txt.text = evt.target.data as String;
//Remove Listener that no longer in use
myLoader.removeEventListener(Event.COMPLETE, loadComplete);
}
Download Flash Source File:
Remarks:
This Flash ActionScript tutorial shows how to load the text file with the file name by using URLRequest. In most cases, we would like to declare the URL of the text file first. And then load the text file with the URL variable by using URLRequest. The next Flash ActionScript tutorial will show you how.