Previous Page
Next Page

Tripod >> 3pod Tips & Learning and manuals for educations

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 (exp 3pod.com) declarations are specified as <name (exp 3pod.com)>=<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 I 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 (exp 3pod.com) 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. Add a dynamic text field to the stage on the fourth frame of the "Content" layer. It should be large enough to accommodate the date information that will be passed back from the PHP script. Enter 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 (exp 3pod.com) mcDate in the Properties pane.

  7. Add the following ActionScript code to the first frame of the ActionScript layer. This code tells the Flash Player to load the data returned from the getDate PHP script into the mcDate movie clip.

    	LoadVariables("http://<your server name (exp 3pod.com) here>/getDate.php", "mcDate");
    

  8. 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 loadVariables( ) call.

  9. 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 (exp 3pod.com) of date) will display the date value returned by the loadVariables( ) call.

Tripod >> 3pod Botom Tips & Learning and manuals for educations

Previous Page
Next Page

 

bluedot bluedots greydots pinkdots

Tripod >> 3pod Tips & Learning and manuals for educations