When building dynamic sites or applications, you may want to load text from an external file instead of hard-coding the text directly into the Flash document. One advantage of dynamically loading content is that you can update the welcome message on your homepage without opening an FLA file, republishing the SWF file or uploading a new SWF file to the server. Instead, you edit the text/HTML document on the server and the updated content is used next time the SWF file is loaded.

The following example displays the contents of a remote text file in a Label component instance.

Full code after the jump.

// ActionScript 3.0
// Import the required component classes.
import fl.controls.Label;
 
/* Create a new Label component instance, set the wordWrap and autoSize properties,
   and add the label to the display list. */
var myLabel:Label = new Label();
myLabel.text = "loading...";
myLabel.width = 530;
myLabel.wordWrap = true;
myLabel.autoSize = TextFieldAutoSize.LEFT;
myLabel.move(10, 10);
addChild(myLabel);
 
// Create a new UILoader instance, add an event listener, and load the remote text file.
var myURLLoader:URLLoader = new URLLoader();
myURLLoader.addEventListener(Event.COMPLETE, completeHandler);
myURLLoader.load(new URLRequest("http://www.helpexamples.com/flash/lorem.txt"));
 
/* Handler function for the URLLoader object. This function sets the text in the label
   instance to the contents of the remote text file. */
function completeHandler(evt:Event):void {
    var txt:String = URLLoader(evt.currentTarget).data as String;
    myLabel.text = txt;
}

For more information on the Flash/ActionScript 3.0 Label component, see the “Using the Label component” Flash Quick Start on Adobe.com.

Tagged with:
 

5 Responses to Loading an external text file into a label instance in Flash using ActionScript 3.0

  1. This is great site, Peter! Thanks for sharing these examples!

  2. 小小菜鸟 says:

    the domain name is very very nice,
    hope it as good as flexexamples.com

    Peter!thanks u

  3. Luc St-Arnaud says:

    Hello,

    I am building a flash file with multiple scenes and with a large amount of animated text by scene (Imagine a power point presentation of 20 pages with interactive navigation but in Flash)

    Then I want to be able to translate this text content in 3 different languages.
    As I don’t want to create 3 different Flash files, I will need an external file
    with the text that I can change easily.

    How would you do that?

    Best regards

    Luc

  4. glasnost says:

    Well Done! I Like it!

  5. seb says:

    Hi do you know how to change the font on the label ?

    please let me now sebbesoder@hotmail.com

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Spam Protection by WP-SpamFree