Scrolling dynamically loaded text in Macromedia Flash MX
Note:Before reading this TechNote you should be comfortable with creating dynamic text fields in Macromedia Flash MX, using the ScrollBar component and working in Expert mode in the Actions panel. Begin by reading Create scrolling text in Flash MX (TechNote 16142).
Loading external text to a dynamic text field
An example of external text using the ScrollBar component.
Requires Macromedia Flash Player 6 to view.
Download Windows source file dynamic_text_MX.zip (44K)
Download Macintosh source file dynamic_text_MX.sit (44K)
In Macromedia Flash 5 and earlier, data from an external text source was loaded using the loadVariables command. The dynamic text field is given a variable name, and the text file contains text that sets that variable name. For example, if the text field variable name is "myText" then the text file may look like this (spaces have been URL encoded):
myText=This%20is%20a%20lengthy%20variable.
Loading text into a dynamic text field in Flash MX is done differently. In Flash MX text is it's own ActionScript object, so a text field can have both a variable name and an instance name (seeFlash MX Text fields have instance names and variable names, TechNote 16184, for details). Flash MX also introduces a more sophisticated action for loading external data called loadVars(). These two methods are combined to load external text to a scrolling text field created using the ScrollBar component.
| 1 | Create a scrolling dynamic text field using the techniques in Create scrolling text in Flash MX (TechNote 16142). Note:Do NOT give the text field a variable name. But do give it an instance name. In this example the instance name is 'scroller' and line type popup is set to 'multiline'.
|
| 2 | Create an external text file to hold the data. In this example the text file is named "test.txt" and is in the same directory as the final SWF. The format of the text should be name=value pairs, separate by ampersands. For example, in this sample file the text file begins "var1=All dynamic..." |
| 3 | Next add the ActionScript to load the external text file. This script uses the new loadVar() action, and is assigned as a frame script.
|
| 4 | Load external text into the loadVars object:
|
| 5 | Check for a successful load of the data. If the data is loaded then the text method of the Text object can be used to assign the data to the text field: //assign a function which fires when the data is loaded:loadVarsText.onLoad = function (success){ if (success){ trace ("done loading"); //Now that we know the data is loaded, //set the text content of the Text Field //with the instance name "scroller" equal to the //contents of the variable scroller.text = this.var1 }else{ trace ("not loaded"); } }
Note:It isn't absolutely necessary to assign the text to the Text field instance at this time. This could be done in a later frame. But it's a good idea to create a function to insure that the data has loaded. In this example the data is held inside the loadVarsText object, so it can be called at any time using:
|
Using HTML formatting
To use html formatting with dynamically loaded text, use the TextField.htmlText property. This allows Flash MX to parse the HTML tags that are contained in the loaded text.
| 1 | Set the TextField.html property to true to identify the text field as having HTML formatting. Using the above example:
|
| 2 | Use the TextField.htmlText property instead of TextField.text to set the variable:
The final script for dynamically loaded text with HTML formatting //assign a function which fires when the data is loaded:loadVarsText.onLoad = function (success){ if (success){ trace ("done loading"); //Now that we know the data is loaded, //set the text content of the Text Field //with the instance name "scroller" equal to the //contents of the variable
|
This content requires Flash
To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.
Download the free Flash Player now!
