Flash AS3 Tutorial: Handle Error During Load External Text File (2)
In previous Flash ActionScript tutorial, we checked the IO_ERROR and SECURITY_ERROR during the loading external text file process. In this ActionScript tutorial, the HTTPStatusEvent will also be checked during the loading external text file process.
Flash Tutorial Content:
The previous ActionScript tutorial check both IOErrorEvent and SecutiryErrorEvent when an error happens. Some people would also check the status of HTTPStatusEvent. This ActionScript tutorial also check HTTPStatusEvent.
The complete Flash Movie is shown as above, you may try how it works before you start this tutorial. Select the Flash Movie and click on the Play button.
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) {
//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) {
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, loadError1)
myLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadError1)
myLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, loadError2)
function loadError1(event:ErrorEvent):void {
output_txt.text = "Sorry that there is an error during the loading of an external text file. The error is:" + "\n" + event.text;
}
function loadError2(event:HTTPStatusEvent):void {
output_txt.text = "Sorry that there may be an error during the loading of an external text file. The error is:" + "\n" + event.status;
}
Download Flash Source File:
Text File: application.txt
Remarks:
We rarely load texts only in Flash application. The loading usually include both texts and images. The next Flash ActionScript tutorial will show how to link to an external image in the variable of the text file.