About loading text and variables into text fields 395
To use LoadVars to populate a text field with external text:
1. Create a new Flash document and save it as loadvarsText.fla.
2. Add the following ActionScript to Frame 1 of the Timeline:
this.createTextField("my_txt", 10, 10, 10, 320, 100);
my_txt.autoSize = "left";
my_txt.border = true;
my_txt.multiline = true;
my_txt.wordWrap = true;
var lorem_lv:LoadVars = new LoadVars();
lorem_lv.onData = function (src:String):Void {
if (src != undefined) {
my_txt.text = src;
} else {
my_txt.text = "Unable to load external file.";
}
}
lorem_lv.load("http://www.helpexamples.com/flash/lorem.txt");
The first block of code in the previous snippet creates a new text field on the Stage and
enables multiline and word wrapping. The second block of code defines a new LoadVars
object that is used to load a text file (lorem.txt) from a remote web server and display its
contents into the my_txt text field created earlier.
3. Save the Flash document and select Control > Test Movie to test the SWF file.
After a slight delay, Flash displays the contents of the remote file in the text field on
the Stage.
For information on security, see Chapter 17, “Understanding Security.”