Recipe 22.9. Accessing External Data


Problem

You want to load external data into a Flash Lite movie.

Solution

Utilize the loadVariables( ) function in conjunction with a server-side script.

Discussion

One of the exciting features of the Flash Lite 1.1 player is its ability to access dynamic data via the Internet. You can develop Flash Lite movies that incorporate frequently changing data, such as sports scores, movie times or weather conditions. Though this technique is not appropriate for all applications, the bulk of the most popular Flash Lite applications incorporate some amount of dynamic data.

The ActionScript function loadVariables( ) is used to achieve this goal. loadVariables( ) sends a request to a specified URL, and loads the returned data into a specified movie clip as ActionScript variables that you can then reference in your movie.

Flash Lite expects the data returned via a loadVariables( ) call to be in a special format called URL-encoded text. Alphanumeric characters do not require special formatting; however, most punctuation and whitespace characters do. As an example, a single-space character is converted to a plus sign (+) in URL-encoded text.

Conveniently, all server-side scripting languages (ColdFusion, PHP, ASP, and so on) have built-in functions to format the text for you. For this example, I will use PHP to pass the current month, day, and year to a Flash Lite movie. The Flash Lite movie will display this information in a dynamic text field on the stage. The first step is to create the PHP script that will respond to the loadVariables( ) call.

  1. Create a new text document in a text editor program (NotePad on Windows or TextEdit on Mac OS X will work fine).

  2. Enter the following code into the document:

     <?php $date = date("F j, Y"); $dateURL = urlencode($date); header("Content-type: application/x-www-form-urlencoded"); print "date=" . $dateURL . "&loaded=1"; ?> 

    This script first stores the current date in the variable $date. It then URL-encodes the date, and stores the URL-encoded value in the variable $dateURL. The third line sends the appropriate HTTP header for URL-encoded text. Finally, it prints out two variables to be returned to the Flash Lite moviedate and loaded. Note the format of the return string. Variable name declarations are specified as <name>=<value> pairs and each declaration is separated by the ampersand character. This is the same format as used in URL query strings and when passing variables to a Flash movie in a web page using FlashVars.

  3. Upload the document to your web server. Note that your web server must be configured to serve PHP files. If you do not have access to a web server, or would rather not go through the hassle, you can call this script using http://www.rightactionscript.com/fcb/getDate.phpI have put this script online for the purpose of this exercise.

The next step is to create the Flash Lite movie that will load this data:

  1. Open a new Flash Lite document.

  2. If it's not already there, create a new layer called ActionScript. Name the other layer "Content." Note that if you created the document using the device templates method in Flash 8, the new movie opens with two layers pre-initialized and named "Content" and "ActionScript."

  3. Add a static text field to the first frame of the "Content" layer with the text "Loading…."

  4. Insert a blank keyframe on the fourth frame of the "Content" layer (Insert Timeline Blank Keyframe).

  5. date in the Var property.

  6. Select this text field and convert it to a movie clip (Modify Convert to Symbol…or F8). Select Movie Clip as the type, and give this movie clip the instance name mcDate in the Properties pane.


  7. Insert a blank keyframe on the third frame of the Actions layer (Insert Timeline Blank Keyframe). We will use this keyframe to test to see whether the data has been returned from our

    Add the following ActionScript code to the third frame of the Actions layer:

     if(mcDate:loaded ne "1") { gotoAndPlay(2); } else { gotoAndStop(4); } 

This if statement checks whether the variable loaded is not equal to 1. Remember that our PHP script returns two variables: loaded (which is set to 1) and date. If loaded is not equal to 1, we know that the data has not been returned from our loadVariables( ) call, and we tell the playhead to go to frame 2 and play. This step sets up a loop whereby the playhead will check for the loaded variable each time it reaches frame 3. When loaded is finally set to 1, the playhead will advance to frame 4 and will stop. As the mcDate clip will also have been assigned a value for the variable date, the dynamic text field we created (which has a variable name of date) will display the date value returned by the loadVariables( ) call.




Flash 8 Cookbook
Flash 8 Cookbook (Cookbooks (OReilly))
ISBN: 0596102402
EAN: 2147483647
Year: 2007
Pages: 336
Authors: Joey Lott

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