Data Transfer - text
Top  Previous  Next


Description
Demonstrates how data can be transferred to and from .htm, .asp, .php or text files that are resident on a server.

Aim
Introduces the methods loadVariables() and shows how you can load an external .txt file into your movie at runtime.


1. Create a New movie and save it as "mytxtmessage.swi". Insert a Dynamic Text object. In the 'Dimensions' options, untick the Auto-Height icon panels-object-text-autosizeheight and choose 'Lines' rather than 'Height' - Set it for several lines to accommodate extra text. Name the text object 'message' and tick the 'Target' option as shown below:

scripttute13_1

2. In the 'Formatting' options, make sure the panels-object-text-wraptext 'Word-Wrap' option is selected. In the 'Advanced' options, press the panels-object-text-blackborder 'Black border with white background' option and assign a variable "mymessage" to this text object as shown below:

scripttute13_2a scripttute13_2b

3. Use the Modify menu and select Grouping | Group as Sprite, then name the sprite "messages" on the 'Sprite' panel.

4. With the sprite object selected in the 'Outline' panel, open the 'Script' panel and press the 'Add Script' button. Select Events | Frame | onLoad() from the menu.

5. Right-Click on the onLoad() action and select Add Script | Movie Control | Load/unload Sprite | loadVariables(...) from the menu.

scripttute13_5

6. In the loadVariables() options, type "extmessages.txt" in the URL field, and type "this" into the Sprite field. If the option "Load variables only" is not ticked, do so now. The 'Script' panel should look similar to this:

scripttute13_6

7. Next, open Windows Notepad (or any text-editing program) and type what is shown in the image below:

scripttute13_7

Note: Notice that in the image above, the text is defined by two ampersand characters "&". When loading external .txt files into a SWF movie, you are not allowed to use the ampersand character in the text messages, because that character tells the Flash player that the text has ended. So, be sure to use "and" instead of "&" when typing the body of the text. Also, note that we have used the Variable assigned to the Dynamic text object - not the name.

8. After typing the body of the text message, save this file as "extmessages.txt" (which is the URL used above for the loadVariables() action) and note the folder you save this .txt file to.

scripttute13_8

9. In SWiSHmax, open the Tools menu, select Preferences, then open the Player options. Tick the "Specify folder" option and press the browse button to locate the folder the "extmessages.txt" file was saved in on Step #8 above.

scripttute13_9

10. Make sure you are in the 'Layout' panel in SWiSHmax then press the 'Play' button in the Control Toolbar to preview the movie. The message typed in to the .txt file should be displayed inside the Dynamic text field.


Analysis
When the 'Play' button is pressed, the onLoad() event is triggered for the sprite "messages". The loadVariables() action was set to only load variables which will load the variables defined in the .txt file at the specified URL.