Flash Labs > Lab 9: Dynamic Content

Overview

In this lab you will learn how to have the contents of your UI prototype load from an external file. For example, you may want to separate out a button label or instructional text so that a coworker can change it in a text file without having to use the Flash authoring application. You can even separate out variables like the how fast something happens.

Goals

Learn how you can use a data load call to pull information dynamically from an external text file when the SWF file loads.

Example

In this example, the instructions are written in a separate file that you can see here. If you change the contents of this file, you will notice the instructions change in the UI prototype. ...there is still a scary woosh sound when the palette opens.


Steps

Optional: Download the working files for this lab

1. Copy the .fla file from Lab 8 in to a new folder called Lab 9. Rename the file yourname_lab9.fla. Open this new file in Flash.

2. Create a file called instructions.txt (view the file here) that sits in the same folder as the .fla file. In this example, we'll use that file to initialize a Dynamic Text box assigned the variable dInstructionText. In order to do this, the instruction.txt file will need to read as follows:

Instructions: Click on the Color palette's tab. (This information is loaded dynamically.)

3. Now create an object in Flash that will read in this information. You'll need to create an XML object:

readFromFileObject = new XML();

Then you'll need to tell the object to assign the value in the text file to the text box named dInstructionText when you call its "load" function:

readFromFileObject.onData = function(data)
{
dInstructionTextInst.text = data;
};

Finally, you call the load function with the name of the text file:

readFromFileObject.load("instructions.txt");


Simply add all the commands listed in Step 3 in to one of the first frames of your movie and you'll see that the information is loaded dynamically and can be easily updated by anyone with access to the text file.

One note of caution: for optimal performance, make sure that the frame that holds the code for loading the text dynamically isn't part of a gotoAndPlay loop.

Learn More

In this lab we're only dealing with loading text, the method described above works well. But say you want to load a number of different values from a file at once. You'll want to use loadVariables() and delimiters in the text file. You can read up about it here.