Section 11.1. Dynamic Text


11.1. Dynamic Text

Dynamic text is text that you insert into your animation at runtime , when the animation plays on your audience's computer (as opposed to when you create the animation).

Dynamic text's great for adding late-breaking updates to your animation: Think sports scores, weather reports , and news headlines. Dynamic text is also a good way to incorporate customized text into your animation without having to go to all the trouble of reworking the animation itself. For example, using dynamic text, you can create a single, basic animation and switch out the header text at runtime so that three different clients can play the same animation and see three different messages (Figure 11-1).

Loading dynamic text into your animation requires a dynamic text box (you can think of it as a placeholder), some data (in this case, it's a text file, but you could also use data you've calculated using ActionScript), and a simple action that ties the two together. The following example walks you through the process.

Figure 11-1. One animation with three different messages, all thanks to one dynamic text element and three separate .txt files. In addition to text, you can also load images and even whole animations at runtime (Section 11.3.1 and Section 11.3.2, respectively, for details).

To create dynamic text:

  1. Open the Flash file dynamic_text_example.fla .

    You can download this example file from the "Missing CD" page at www.missingmanuals.com/cds.

    If you don't see the Property Inspector, choose Window Properties Properties to display it.

  2. .

    Flash redisplays the Property Inspector to include dynamic textrelated properties.

  3. Click the "Instance name" field and type a short, unique name (such as myDynamicTextInstance ) for this instance of dynamic text (Figure 11-3). Then, in the Variable field, type theActualText , which is the name for the text you're going to associate with this dynamic element .

    Figure 11-2. Flash assumes you want static text (check out the Property Inspector) until you tell it otherwise , which the following steps show how to do.

    The variable name refers to just the dynamic text you'll be creating (as opposed to the whole text element complete with its associated font, color , and other characteristics, which is what the instance name refers to).

    In the next few steps, you'll create the text file you want Flash to pull in at runtime.

    Figure 11-3. Top: At runtime, Flash uses the variable name you choose in the Property Inspector
    Bottom: to tie your dynamic text element to a text file.
  4. Using a text editor program, create a text file named myCustom.txt containing the following single line of text :

     theActualText=Ralph 

    As you can see in Figure 11-3, your text file needs to contain the variable name you typed into the Property Inspector, followed by the equal sign, followed by the text you want Flash to load into your animation at runtime. (In computerese, the term for this standard something=something format is name-value pair .) Your text can include spaces and punctuation.


    Tip: If you like, you can include HTML tags in your text file. (For example, you can surround a bit of text with the HTML tags <FONT COLOR="red" >your text here</FONT> to pull in red text at runtime.) If you do include HTML tags in your text file, though, make sure you click the "Render text as HTML file" icon in the Property Inspector. If you don't, Flash displays your HTML tags instead of interpreting them.
  5. Save your myCustom.txt file in the same folder that you save your Flash file for this example .

    Next, you'll create an action that ties the contents of your dynamic text element to this .txt file.


    Note: Flash lets you name your text file anything you like. But for the purposes of this example, stick to myCustom.txt.
  6. Click Frame 1 to select it .

    You're creating a frame-based action (as opposed to a text elementbased action) because you want Flash to load your dynamic text as soon as it loads your frame.

  7. Choose Window Actions or , if the collapsed Actions panel is visible, click Actions .

    Flash displays (or expands) the Actions panel. Make sure the title of the panel reads "ActionsFrame;" if it doesn't, repeat the previous step.

  8. In the Actions panel, type the following ActionScript code :

     loadVariables("myCustom.txt", this); 

    The above line of ActionScript code tells Flash to load the contents of the myCustom.txt file into this animation. When Flash loads the text file, the contents of the file tell Flash what variable you want to plug text into (the variable theActualText ), and what the text is ( Ralph ).

  9. Test your dynamic text element by choosing Control Test Movie .

    The Flash Player you see in Figure 11-4 appears.

Figure 11-4. If you can't get your dynamic text to work (or just want to save yourself a bit of typing), no sweat; you can find working files for this dynamic text example (dynamic_ text_working.fla and myCustom.txt) online at www.missingmanuals.com/cds.


Note: Flash doesn't require you to create the text file yourself. If you're a programmer type, you can generate it at runtime using a server-side program, or even have someone else create it using a text editor. You can also change where Flash expects to find the text file. (Out of the box, Flash expects to find the text file in the same directory as the .fla file that references it.) For details, search Flash Help for "loadVariables."



Flash 8
Flash Fox and Bono Bear (Chimps) (Chimps Series)
ISBN: 1901737438
EAN: 2147483647
Year: 2006
Pages: 126
Authors: Tessa Moore

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net