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 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.

Please update flash player to view this Flash ActionScript tutorial!

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) {

//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, loadError1)
myLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadError1)
myLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, loadError2)

 

function loadError1(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.text;

}

 

function loadError2(event:HTTPStatusEvent):void {

// Display error message to user in case of loading error.
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:

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

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.