Documente Academic
Documente Profesional
Documente Cultură
Introduction
The FlashVars property of Macromedia Flash Player provides an efficient method of
importing variables into the top level of a movie when first instantiated. This feature requires
Flash Player 6 (or later). For information on passing variables with earlier versions, refer to
Additional Information below.
FlashVars
Placed within the Object (ActiveX) or Embed Tag (Netscape and Macintosh Internet
Explorer), the property "FlashVars" can be used to import root level variables to the movie.
All variables are created before the first frame of the SWF is played. The format of the string
is a set of name=value combinations separated by '&'. Special and/or non-printable characters
can be escaped with a '%' followed by a 2 digit hexadecimal value. A single blank space can
be represented using the '+' sign. For more information about URL encoding, please check
URL Encoding: Reading special characters from a text file (TechNote tn_14143).
All browsers will support string sizes of up to 64KB (65535 bytes) in length. FlashVars must
be assigned in both the OBJECT and EMBED tags in order to work on all browsers.
Object Tag Example:
<PARAM NAME=FlashVars VALUE="foo=Hello%20Worldgraph=first+line
%0Dsecond+line">
Encoding of this string is the same as the page containing it. Internet Explorer is responsible
for providing UTF-16 compliant string on a Windows platform. Likewise, Netscape will
provide a UTF-8 encoded string to the player.
Additional Information
Without FlashVars, variables can be passed to a SWF through HTML tags by appending them
to URL of the SWF on a query string. For a simple example, refer to Different approaches to
setting variables in a Flash movie (TechNote tn_14253). This method can present a problem if
you need the variables immediately, because the SWF must be downloaded and played before
large amounts of data can be requested from the server.
Code :
1. <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#
version=6,0,29,0" width="800" height="100">
2.
<param name="movie" value="menu.swf?onglet=<?php echo $onglet?>" />
3.
<param name="quality" value="high" />
4.
<embed src="menu.swf?onglet=<?php echo $onglet?>" quality="high"
pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/xshockwave-flash" width="800" height="100"></embed>
5. </object>
Set Variable
Use the Set Variable action in Flash. This example uses a frame action, though it can also be
done using a button action.
To set a variable using "Set Variable":
1.
2.
3.
4.
5.
6.
7.
8.
Note: To the right of the "Variable" and "Value fields", there is a pop-up menu, which
contains "String Literal", "Expression", and "Expression Editor". The first time a variable is
set, make sure the variable field is set to "String Literal". For more information creating
expressions, please refer to page 196 of the Flash 4 manual.
Note: A value you assign to a variable will also display in a text field with the same variable
value. (For instance, if "text" is set to "hello", the word "hello" will display in the text field
with variable name "text".)
Load Variables
Variables can be obtained from a remote file by using the action Load Variables. The remote
file can be a text file or a server-side application such as ASP, CGI or ColdFusion. This
example uses a text file as the data source, with the command issued by a frame action.
To obtain external data using Load Variables:
1. Create a text file (using SimpleText or Notepad) containing only the text "text=hello"
without quotes. Save this text file as "text.txt" to your working folder.
2. Open a new document in Flash.
3. Highlight Frame 1 in the timeline.
4. Choose Modify > Frame from the menu.
5. In the Actions tab, click the "+" sign, and choose "Load/Unload Movie" from the popup menu.
6. On the right, choose the" Load Variables into Location" radio button.
7. In the "URL" field, type the name of the text file "text.txt".
8. In the "Level" field, type the number 0. Click OK.
9. Create a text field on the stage with variable name "text". This will demonstrate when
the data has been loaded.
10. Publish the movie to the same folder as the text file "text.txt".
11. When the movie plays Frame 1, the movie will pull the variable and value pair
"text=hello" from your data source into the current timeline. Additional data can be
added to this by using the syntax specified on page 183.
Note: Special characters (such as punctuation, numeric operators, and other nonalphanumeric data) must be URL encoded to be translated into the Flash movie. For more
information, see URL Encoding: Reading special characters from a text file (TechNote
14143).
Using JavaScript
It is possible to pass variables to a Flash movie from JavaScript in the HTML page. This is
just one example of a strategy that can be used in many ways. In the example below, a link in
the HTML page passes a variable when clicked to a text field in the Flash movie. This method
will not work on Internet Explorer for the Macintosh.
To create this sample, do the following:
1.
2.
3.
4.
11. In the EMBED tag, look for the HEIGHT and WIDTH parameters. Insert the
following parameter (if it is not already in there):
NAME=movie
12. After this has been added, insert the follow code directly following this:
swLiveConnect=true
13. Just above the closing body tag ("</body>"), insert this code:
<a href="#" onClick="PassFlash()">Pass The Variable</a>
14. Save the document as "movie.html", replacing the old version of this file.
15. Open "movie.html" in a browser. To pass the variable to the movie, click the text "Pass
the Variable". The value of "text" becomes "hello", and the text field will display this
new value.
Note: A complete description of JavaScript is beyond the scope of this TechNote. Complete
technical documentation for JavaScript is available from DevEdge Online Documentation, as
well as numerous third party JavaScript books and Web sites, such as Webmonkey's
Programming JavaScript. If you use a JavaScript method to communicate with a Flash movie,
additional logic may be necessary to make sure issues such as browser type, layers and such
are considered when implementing a script.
Additional Information
Using variables in Flash can create powerful and scalable movies that can change to reflect
user choices, updates and events. Writing expressions adds new levels of complexity to
authoring in Flash. For more instructions on many Flash scripting and design techniques,
please visit the Flash Support Center and view related TechNotes. Enter keyword topics into
the search field to access a wide variety of articles on beginning and advanced topics.