Defining a Data Process with States


When you manipulate text fields with internal ActionScript properties of the TextField object, the data for the text fields is available for use immediately. Meaning, if you declare a variable and a value for that variable, any text field can be given that value as well. When you want to load external data into a Flash movie, you need to create the appropriate steps, or states, in your movie to make sure that the data is available for use in the Flash movie. For example, suppose you want to retrieve a news article from a Web server, and the text for that article is contained within a variable named article_1. You can't use or assign the value of article_1 to any other Flash element unless the article has fully downloaded to the Flash movie.

So, how do you know when data is available in a Flash movie? Any Flash movie that relies on data exchange between the Flash movie and the Web server should contain four separate states:

  • An input state to gather the information from the user or the movie

  • A send state in which a Flash action sends the data out of the movie

  • A wait state during which the data downloads to the movie

  • An output state in which the data can be used by the Flash movie in text fields and other ActionScript code

Input State

The first step for data exchange requires that you have something to send out of the Flash movie. The input can be a Flash form into which a user types text. The data could be environment variables, such as the time of the day, or the Flash Player version. There could be various substeps in the input state, such as multiple forms or the completion of a quiz to calculate a test score that will be sent to the Web server.

Note 

Not all data transactions require input. For example, if you need to retrieve the latest stock quotes or news article from a Web server application, you may simply need to send a request for that data without providing any additional information to the Web server application.

Send State

After the input data has been set or retrieved in the Flash movie, you're ready to send the data to another host, such as an application or script on your Web server. You can use the following actions or object methods to send data out of the Flash movie:

  • getURL()

  • loadVariables() or MovieClip.loadVariables()

  • loadMovie() or MovieClip.loadMovie()

  • LoadVars.load()

  • LoadVars.send() or LoadVars.sendAndLoad()

  • XML.load()

  • XML.send() or XML.sendAndLoad()

  • XMLSocket.send()

  • Flash Remoting or Flash Communication Server calls

Of these actions, getURL(), LoadVars.send(), and XML.send() are restricted to a one-way data path; that is, you can only send data out with these actions — you cannot receive external data with them. getURL() must target the sought URL to the current browser (or frame) or a new browser window. In many situations, you may only need to send data out of the Flash movie without needing to receive any further data. To send information with the user's e-mail client, you can use a simple mailto: URL in a getURL() action on a Button instance. Note that the image from book character indicates a continuation of the same line of code:

 var btSend:Button; btSend.onRelease = function():Void {   var sEmail:String = "admin@server.com";   var sSubject:String = escape("Visitor Feedback");   var sBody:String = escape("Please let us know how you feel.");   getURL("mailto:" + sEmail + "?subject=" + sSubject + image from book     "&body=" + sBody); }; 

In the preceding code block, the variables sEmail, sSubject, and sBody are inserted into the getURL() action. Note that you can automatically set subject and body text for the e-mail message as well. To add specific variables to a URL string, you should use the escape() function in ActionScript, which converts illegal URL characters such as spaces and question marks into URL form-encoded text (for example, a space is converted into %20).

Caution 

Not all e-mail clients support the passing of subject and body variables. Be sure to test this code with your targeted e-mail clients.

Wait State

If you are sending data from the Flash movie with loadVariables(), loadMovie(), or any of the object load() or sendAndLoad() methods, you need to know when the requested data is received by the script or application running on your Web server. Usually, you will build server-side scripts that return some data back to the Flash movie upon successful receipt of data from Flash. If you're building Flash Player 4-compatible movies, one way to detect the receipt of data into the Flash movie is to use a terminal tag — a name/value pair in the downloaded data that indicates the end of the data string. For example, if the value of the text property for the tOuput instance that you used in the last section was converted to a name/value pair in a .txt file (as URL form-encoded text), the value would appear as the following (URL-converted characters are shown in bold, and the terminal tag is underlined):

 article=WANTED%3A%20Flash%20Input%20%26%20Output%0AA%20start%2Dup%20Dot %20com%20company%20is%20looking%20for%20a%20qualified%20web%20technolog y%20that%20will%20present%20text%20input%20and%20output%20to%20web%20vi sitors%20in%20a%20more%20compelling%20animated%20and%20visually%20stunn ing%20environment%20than%20that%20possible%20with%20HTML%2E%20Please%20 call%3A%0A%0A1%2D800%2D555%2DCODE&success=1 

At the end of this line of text (or at the very end of a long line of variables), we have inserted a terminal tag success=1. With this variable in place, you can set up a frame loop within a Flash movie to detect the existence (loading) of the terminal tag variable. After the terminal tag is loaded, the Flash movie will be directed to the appropriate output state.

All wait states should have a timeout condition: If the data fails to load within a certain time frame, you will assume the Web server (or script) is not functioning correctly. If the timeout condition proves true, the Flash movie will go to the appropriate output state. You create a wait state for the Flash form in the next section.

Output State

The final step in a data exchange is the actual display of any received data in the Flash movie. However, as indicated in the last state, there are two separate output states: a success display or an error display. If the data was properly received during the wait state, the Flash movie will display the success output state. If the server failed to return any data to the Flash movie, the movie will display an error output state, indicating that there was a problem with the server.




Macromedia Flash 8 Bible
Macromedia Flash8 Bible
ISBN: 0471746762
EAN: 2147483647
Year: 2006
Pages: 395

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