View Single Post
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator 17 Creative Assets 2006-11-04 #5 Old  
004 - Gathering FlashVars
 
If you send variables to your Flash movie using flashVars the following function could be used to gather all of the variables up into an object. Number values are converted to true numbers, and you can also name your flashVars so they are recreated as arrays.

Here's the function:

ActionScript Code:
  1. function getFlashVars():Object {
  2.     var o:Object = {};
  3.     var n:String;
  4.     var i:Number;
  5.     var s:String;
  6.     for (s in _level0) {
  7.         if (s.substr(0,3) == "fv_") {
  8.             n = s.substr(3);
  9.             if (n.indexOf("_") > -1) {
  10.                 i = Number(n.split("_")[1]);
  11.                 n = n.split("_")[0];
  12.                 if (o[n] == null) {
  13.                     o[n] = [];
  14.                 }
  15.                 if (isNaN(_level0[s])) {
  16.                     o[n][i] = unescape(_level0[s]);
  17.                 }
  18.                 else {
  19.                     o[n][i] = Number(_level0[s]);
  20.                 }
  21.             }
  22.             else {
  23.                 if (isNaN(_level0[s])) {
  24.                     o[n] = unescape(_level0[s]);
  25.                 }
  26.                 else {
  27.                     o[n] = Number(_level0[s]);
  28.                 }
  29.             }
  30.         }
  31.     }
  32.     return o;
  33. }
Here are a few examples of use. Note that each flashVar name is prefixed with fv_

01 - Basic example

HTML
Code:
<param name="flashVars" value="fv_foo=hello&fv_bob=123" />
AS
ActionScript Code:
  1. var info:Object = getFlashVars();
  2.  
  3. trace( info.foo ); // output: hello
  4. trace( info.bob ); // output: 123
  5. trace( typeof info.bob ); // output: number
  6.  

02 - Sending Arrays

To send an array to Flash you simply add _0 _1 _2 etc to the end of your variable&nbsp;name.

HTML
Code:
<param name="flashVars" value="fv_items_0=apple&fv_items_1=banana" />
AS
ActionScript Code:
  1. var info:Object = getFlashVars();
  2.  
  3. trace( info.items[0] ); // output: apple
  4. trace( info.items[1] ); // output: banana
  5.