Home | Flash AS3 Tutorials | Flash Animation Tutorials | Store

Flash ActionScript Tutorial: Error During Load External Text File

In previous Flash ActionScript tutorials, we assume that the loading process of the external text files was smooth and completed without any problem. This come another question. What if an error happens during the loading process?

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

Sometimes errors may happen during the loading process, for example, you change the file names of the external text file but forget to update the ActionScript codes. Therefore this is always better to include error checking in your ActionScript codes.

The complete Flash Movie is shown as above, you may try how it works before you start this Flash ActionScript tutorial. Select the Flash Movie and click on the Play button. The file name of the external text file is wrong due to typing mistake. Therefore the external text File cannot be loaded properly. An annoying error message box may be pop up. However the user do not know what happens. You properly came across this kind of error before.

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;

 

function checkComplete(evt:MouseEvent) {

//Load the text file by using URLRequest
//Can be local file or URL of webpage
//WRONG file name of the external text file
myLoader.load(new URLRequest("application1.txt"));

}

 

enter_btn.addEventListener(MouseEvent.CLICK, checkComplete);

 

//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-4.fla

Remarks:

The above ActionScript codes will not display the content of the external text file when there is an error during the loading process. An annoying error message box may be pop up. The users do not know what happen. Therefore this is better to inform the users that an error happens for the Flash Movie. And the users can inform the website Administrator about the error. The next Flash ActionScript tutorial will show how to do that.