Home | Flash AS3 Tutorials | Flash Animation Tutorials | Store

Flash AS3 Tutorial: Handle Error During Load External Text File (1)

As we also indicated in the Flash ActionScript tutorials of loading external swf flash files and external images, error handling should be included when loading external files. In fact error might happen during the loading external files process. Therefore this is better to inform the users that some problem happens during the loading external files process. In this Flash ActionScript tutorial, we will check the IO_ERROR and SECURITY_ERROR during the loading process.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

The contents of the external text file will not be displayed when there is an error during the loading process. 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 complete Flash Movie is shown as above, you may try how it works before you start this ActionScript tutorial. Select the Flash Movie and click on the Play button. You may change the file name of the external text file therefore the content of the external text File cannot be displayed on the MainTimeline. Unlike previous Flash ActionScript tutorial, the error message will be shown on the message box.

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
myLoader.load(new URLRequest("application.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);

}

 

//////////////////////////////
///// ERROR HANDLING /////
/////////////////////////////

// Listen error events for the loading process
// Call loadError function in case an error appear
myLoader.addEventListener(IOErrorEvent.IO_ERROR, loadError)
myLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadError)

 

function loadError(event:ErrorEvent):void {

// Display error message to user in case of loading error.
output_txt.text = "Sorry that there is an error during the loading of an external text file. The error is:" + "\n" + event;

}

Download Flash Source File:

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

Text File: application.txt

Remarks:

The above Flash ActionScript codes will check both IOErrorEvent and SecutiryErrorEvent when an error happens. Some people would also check the status of HTTPStatusEvent. The next Flash ActionScript tutorial will also check HTTPStatusEvent.