Loading Text from External txt File with HTML Formatting

Loading Text from External txt File with HTML Formatting

Flash CS4 ActionScript 3.0 Tutorial


How to Add External Text Files into Flash



Text files have multiple uses in Flash. Flash uses the text file information to fill Dynamic text fields. You can create an external txt files using most text editor programs (avoid Word). The advantage to external text files is that they can be quickly easily updated, keeping your content current.

In this example we will create a Flash file with a Dynamic text field and an external txt file. The text from the external file will appear in the text field. If you wish to change the content of the txt file, simply replace the current file with an updated file on the server.

For additional information on importing txt files, see topic, Loading Text from External txt File.

Tutorial Elements

Text file with name ExternalText01.txt. File should be located at the same level as your fla file.


Step One

Open a new Flash document and save the file as Main.fla in a Project folder.

Step Two

Add a new layer and name the top layer Actions and the lower layer Content.

Step Three

Click the first frame of the Content layer and add an empty Dynamic text field to the stage with the Text tool.

Step Four

Using the Select tool, click the box and set the font Size to 12 in the Properties panel.

Step Five

With the Properties panel still open, set the width (w) and height (h) to 200 and 400. Behavior should be set to Multiline.

Step Six

Enter textBox01_txt for the instance name in the Properties panel.

Step Seven

Open a new text document and copy paste the following text:

<b>External Text File Concepts</b>

This text is loaded to the stage from an external txt file that <i>includes</i>:<li>Returns





Step Eight

Save your txt file next to the Main.fla file inside the Project folder. Name the file, ExternalText01.txt.

Step Nine

Open the ActionScript panel for the only keyframe in the Actions layer by Option double-clicking (PC - Alt double-clicking) the keyframe.

Step Ten

Copy/Paste the following code in the ActionScript Panel:

var extLoader:URLLoader=new URLLoader();

extLoader.load(new URLRequest("ExternalText01.txt"));

extLoader.addEventListener(Event.COMPLETE, onLoaded01);

function onLoaded01(e:Event):void {



Step Eleven

Click the Auto Format button to format the code.

Step Twelve

Save your Main.fla file

Step Thirteen

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). The text box appear on the stage with the contents of the external txt file.

Dissecting the Code

The html text at Step Seven performs several formatting tasks. The first line is rendered as bold with the <b> and </b> tags. The word includes becomes italicized and the words Returns, Bold, Bullets and Italics display as a list.

The first line of code at Step Ten establishes a new URLoader variable named extLoader.

The second line loads the ExternalText01.txt file in the loader.

Line three adds an event listener to detect if the txt data has finished downloading. If complete, the onLoaded01 function is run.

Inside the function, extTextFile, the Dynamic text field is loaded with the external text and evaluated as html.