Task 6: Reading Text from a File
Flash can load XML files and text streams containing name/value pairs, but sometimes it's desirable to pull up a few paragraphs of regular text from a file and display them. There's no obvious way to do this, short of writing a text file in the form "mytext=the rest of the words" or wrapping the paragraphs in an arbitrary XML tag (major overkill).
Flash MX Solution
The new loadVars object is a better way of loading name/value pairs from a text file or server-side script. It has an undocumented method called onData (distinct from onLoad) that returns the entire contents of the file. By creating a new loadVars object and writing a simple onData function, you can feed the raw text file into a field for display.
Try It
To load a product description direct from a text file, complete the following steps:
Create a text file in NotePad or TextEdit (or any text editor), and type or paste in the following paragraph:
Macromedia Flash MX is the fastest way to create rich Internet content and applications with a better return on investment. Powerful video, multimedia, and application development features allow the creation of rich user interfaces, online advertising, eLearning courses, and enterprise application front-ends. Deploy consistently to over 436 million Internet users across all major platforms and devices with the leading rich client, Macromedia Flash Player.
Save the text file as Flash.txt and place it in a new folder.
Open up a new Flash document, then save it as LoadVars.fla, in the same folder as the text file.
Use the Text Tool to draw out a dynamic field large enough to hold the loaded paragraph. Assign the field the instance name myField, and use the Properties inspector to ensure it is dynamic and multi-line (so the text will wrap).
Select the first frame on the timeline, and open the Actions panel.
Type or paste in the following code to load the text file:
loadText = new LoadVars(); loadText.onData = function(raw) { myField.text = raw }; loadText.load("Flash.txt");
Upon export, the contents of Flash.txt will show up in the dynamic text field.