Flash ActionScript Tutorial: Load External Text File and Image
This Flash ActionScript tutorial show how to load an external text file with variables. However the variable in the text file also include link to an external image.
Flash Tutorial Content:
In previous Flash ActionScript tutorials, we only load the text contents of the external text file into the text fields of Flash Movie. Actually we can also put a "text link" to the variable of the text file. Therefore the external image will be loaded when the external text file is loaded to the Flash Movie.
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.
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
//Content of external text file:
/*
Candidate=Alex
&Post=Production Engineer
&Comment=Recommend Hire
&Pic=cat.png
*/
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;
//Load the image
var loader:Loader = new Loader()
pic1_mc.addChild(loader)
loader.load(new URLRequest(evt.target.data.Pic))
//Remove Listener that no longer in use
myLoader.removeEventListener(Event.COMPLETE, loadComplete);
}
//////////////////////////////
///// ERROR HANDLING /////
/////////////////////////////
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.