In this tutorial you will learn how to load external text into a dynamic text field in Flash. You will need to use the URLRequest and the URLLoader classes. The URLRequest class is used to capture all the request information, and the URLLoader is used to load up the request information. More information on these classes can be found at the AS3 component reference.
Load external text in Actionscript 3.0
Open a Flash AS3 file.
Then open up notepad or an equivalent program and write the message: “This is externally loaded text” and save the file with the name “external_text” in the same location as the fla file.
Select the text tool and drag and dynamic text field on the stage like below:
Using the selection tool (V) select the dynamic text field and give the following instance name: “my_txt” as shown below:
On the timeline insert a new layer called “Actions”. Select the first frame and hit F9 to open the Actions panel and enter the following code:
var my_req:URLRequest = new URLRequest("external_text.txt");
var my_loader:URLLoader = new URLLoader();
my_txt.text = my_loader.data;
1. This creates new instances of the URLRequest and the URLLoader classes. Note that the URLRequest has the name of the text file inside the parenthesis. The text file also has to be in the same location as fla file.
2. This is an event listener to listen to when the text file has finished loading using the ‘Event.COMPLETE’ event. The load method receives the data from the text file.
3. This is the load text function sets the text from the text file to the dynamic text field.
Test your loaded external text Ctrl + Enter. You should get the following message below:
You should now be able to load external text.