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.
5 Responses to Loading an external text file into a label instance in Flash using ActionScript 3.0
Leave a Reply Cancel reply
Recent Posts
- Getting started with the TLFTextField class in ActionScript 3.0 and Flash CS5
- Adding tick marks to a Slider control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Creating a custom context menu item in Flash using ActionScript 3.0
- Rotating a Sprite object around its x-axis in Flash using ActionScript 3.0 and Flash Player 10
- Detecting when the user changes the color in a ColorPicker control in Flash using ActionScript 3.0
- Getting the currently selected color as a hexadecimal value on a ColorPicker control in Flash using ActionScript 3.0
- Toggling the text field on the ColorPicker control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Setting the number of columns on a ColorPicker control in Flash using ActionScript 3.0
Categories
- Bitmap (1)
- Components (72)
- Button (19)
- CheckBox (2)
- ColorPicker (6)
- ComboBox (1)
- DataGrid (8)
- FLVPlayback (7)
- Label (9)
- ProgressBar (2)
- Slider (3)
- TextArea (1)
- TextInput (7)
- UILoader (7)
- ContextMenu (1)
- Embed (4)
- ExternalInterface (2)
- Flex (7)
- Font (2)
- General (5)
- Graphics (2)
- JSFL (14)
- Loader (3)
- LoadVars (3)
- Microphone (1)
- migration (12)
- MovieClip (1)
- MovieClipLoader (1)
- Sound (2)
- TextField (1)
- TLFTextField (1)
- TransitionManager (1)
- Tween (1)
- Uncategorized (1)
- URLLoader (4)
- URLVariables (1)
- Video (1)
- XML (2)
Advertising
" RT @OReillyMedia: #Ebook Deal/Day: Learning JavaScript Design Patterns -
$11.99
(Save 50%)
http://t.co/ilcmSDv6 " — pdehaan


This is great site, Peter! Thanks for sharing these examples!
the domain name is very very nice,
hope it as good as flexexamples.com
Peter!thanks u
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
Well Done! I Like it!
Hi do you know how to change the font on the label ?
please let me now sebbesoder@hotmail.com