Flylib.com

Books Software

 
 
 

2.6 Loading External Variables

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin  Moock
Chapter 2.  Variables

2.6 Loading External Variables

While most variables are created directly inside Flash, it's also common to load variables from an external text file, server script, or web page. Loading external variables does not mean simply assigning a value from an external source to existing variables. Loading external variables actually creates new variables at runtime. The following entries in the ActionScript Language Reference explain various variable-loading techniques:

  • LoadVars

  • loadVariables( )

  • fscommand( )

Additionally, in some browsers, JavaScript can set a variable in Flash at runtime using the syntax:



movieObj


.SetVariable("/someClip:firstName", "Colin");

where movieObj is an object reference to the Flash movie embedded in the page. This technique works only in the following browsers:

  • Internet Explorer on Windows

  • Netscape 4 on Windows, Macintosh, and Linux

  • Netscape 6.2 with Flash Player 6.0.40.0 (or higher) on Windows, Macintosh, and Linux

For complete details, see:

http://www.moock.org/webdesign/flash/fscommand/

Similarly, an HTML document's <OBJECT> and <EMBED> tags can create variables in a Flash movie, when the movie initially loads, via the FlashVars parameter or a query string. Variables passed to a Flash movie via FlashVars or a query string must be URL-encoded according to the rules described in the LoadVars class in the Language Reference . The following code uses the FlashVars parameter to create the variables authorName and bookName with the values "Colin Moock" and "ASDG" on the main timeline of a movie called main.swf :

<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
 CODEBASE="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"
 WIDTH="550"
 HEIGHT="400">
 <PARAM NAME=movie VALUE="main.swf">

<PARAM NAME=FlashVars VALUE="authorName=Colin+Moock&bookName=ASDG">



<EMBED src="main.swf"

FlashVars="authorName=Colin+Moock&bookName=ASDG"

WIDTH="550" 
 HEIGHT="400" 
 TYPE="application/x-shockwave-flash"
 PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">
 </EMBED>
</OBJECT>

The FlashVars parameter requires Flash Player 6 or later and can pass a maximum of 63 KB of data to Flash. To pass data from HTML to earlier Flash Players, append the parameters to the URL of the .swf filename, as shown in the following example. In this case, the data transfer limit depends on the web server.

<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
 codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"
 WIDTH="550"
 HEIGHT="400">

<PARAM NAME=movie VALUE="main.swf?authorName=Colin+Moock">



<EMBED

src="main.swf?authorName=Colin+Moock"

WIDTH="550" 
 HEIGHT="400" 
 TYPE="application/x-shockwave-flash"
 PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">
 </EMBED>
</OBJECT>

The passed variables are always created on the movie's main timeline. There is no direct way to set variables in a nested movie clip using FlashVars or a query string. See Appendix H for more details on the <OBJECT> and <EMBED> tags.

 

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 2.  Variables

2.7 Some Applied Examples

We've had an awful lot of variable theory. The following examples provide three variable-centric code samples that show some of these concepts in use. Refer to the comments within the code for an explanation of each line of code.

Example 2-6 sends the playhead of a movie clip to a random destination.

Example 2-6. Send the playhead to a random frame on the current timeline
var randomFrame;                 // Stores the randomly picked frame number
var numFrames;                   // Stores the total number of frames in the clip
numFrames = this._totalframes;   // Store the current movie clip's 
                                 //


_totalframes


property in


numFrames


// Pick a random frame
randomFrame = Math.floor(Math.random( ) * numFrames + 1);
this.gotoAndStop(randomFrame);   // Send playhead of current movie clip to
                                 // chosen random frame

Example 2-7 determines the distance between two clips. A working version of this example is available from the online Code Depot.

Example 2-7. Calculate the distance between two movie clips
var c;                    // A convenient reference to the


circle


clip object
var s;                    // A convenient reference to the


square


clip object
var deltaX;               // The horizontal distance between


c


and


s


var deltaY;               // The vertical distance between


c


and


s


var dist;                 // The total distance between


c


and


s


c = _root.circle;         // Get reference to the


circle


clip
s = _root.square;         // Get reference to the


square


clip
deltaX = c._x - s._x;     // Compute the horizontal distance between the clips
deltaY = c._y - s._y;     // Compute the vertical distance between the clips
// The distance is the square root of (


deltaX


squared plus


deltaY


squared).
dist = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
// Using variables, the code above creates tidy references that
// are much more readable than the alternative, shown below:
dist = Math.sqrt(((_root.circle._x - _root.square._x) * (_root.circle._x - 
_root.square._x)) + ((_root.circle._y - _root.square._y) * (_root.circle._y - 
_root.square._y)));

Example 2-8 converts from Fahrenheit to Celsius. A working version showing bi-directional conversion is available in the online Code Depot.

Example 2-8. A Fahrenheit/Celsius temperature converter
// Create variables
var fahrenheit;           // Temperature in Fahrenheit
var result;               // The value resulting from conversion
   
// Initialize variables
fahrenheit = 451;         // Set a Fahrenheit temperature
 
// Calculate the Celsius equivalent
result = (fahrenheit - 32) / 1.8;
// Display the result
trace (fahrenheit + " degrees Fahrenheit is " + result + " degrees Celsius.");