Using FlashVars with the LoaderInfo class

When embedding flash in a web page, we sometimes want to pass some setup information to the swf in the form of flash variables. To obtain the flash variables from the html we must wait for flash to full initialize its internal properties, different browsers can take varying amounts of time to do this, and when lots of setup information is passed, this can take significantly longer.

The setup information is transfered in the form of the flashVars.

            flashvars.varName  = 'firstVar';
            flashvars.varName2 = 'firstVar2';

Typically flash developers use the SwfObject javascript class to embed flash cross browser. Below is an html example using SwfObject, you will need to download SwfObject js file and place it in a js subdirectory. In this example we are setting two flashVars.

So here is our html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"

<html lang="en">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="JLM" content="example of using FlashVars">
    <script type="text/javascript" src="js/swfobject.js"></script>
        <script type="text/javascript">
            var flashvars = {};
            flashvars.varName  = 'firstVar';
            flashvars.varName2 = 'firstVar2';
            var params = {
                            menu: 'false'
            var attributes = {};
            swfobject.embedSWF("FlashVarTest.swf", "flashcontent", "800", "600", "10.0.0","expressInstall.swf" , flashvars, params, attributes);
        <div id="flashcontent">
            <h1>Alternative content</h1>
                <a href="http://www.adobe.com/go/getflashplayer">
                    <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />

How to detect when Flash is initialized

The explanation below doesn't show the correct way to wait for loaderInfo . Rather than polling for ready state on timer, listen for Event.ADDED_TO_STAGE on flash.Lib.current, then listen to Event.INIT on flash.Lib.current.stage.loaderinfo. However, these events may not be called if they already occurred, so don't listen to either event if flash.Lib.current.stage != null and Std.is( flash.Lib.current.stage.stageWidth , Int ). Below follows the timer polling method, which may be more well tested.
Browsers usually take time to populate some properties flash expects, such as stageWidth, and flashVars.
So there are a few different ways we can check when flash is setup. One way is to set a timer to check when the flashVars dynamic has atleast one value, but this assumes we are using atleast one flashVar and is therefore not ideal.
There are some events in later versions of flash player to help with detection but I don't use them.
How you implement the delay untill flash properties are fully set up is up to you, but this approach below works.

Check stageWidth works well

I find that checking if stageWidth is set is very simple approach that works for all flash9/10 ( as3 - ASM-2 ) players.
//Name file: FlashVarsTest.hx

import flash.display.Stage;
import flash.display.Sprite;
import flash.Lib;
import haxe.Timer;
import flash.display.LoaderInfo;

class FlashVarTest extends Sprite
    public static  var _this:   FlashVarTest;
    private var _timer:         Timer;
    private var _mc:            Sprite;
    private var _flashVars:     Dynamic<String>;
    private var _loaderInfo:    LoaderInfo;
    private function movieStart()
    // get flashVars set in the html
        _flashVars =   Lib.current.loaderInfo.parameters;
        var arr:    Array<String> = Reflect.fields( _flashVars );
        var fvs:    String;
        for( i in 0...arr.length )
            fvs = arr[ i ];
            trace( 'FlashVars.' + fvs + ' = ' + Reflect.field( _flashVars, fvs ) );
    static function main(){ _this = new FlashVarTest (); } private function new()
        _timer = new Timer( 40 );
        _timer.run = isReady;

    // wait for flash browser properties to be initilized
    private function isReady()
        if( Lib.current.stage != null )
            if( Std.is( Lib.current.stage.stageWidth , Int ) )
                _mc = Lib.current;

Now to compile this we can use this hxml file.
-cp ./
-swf FlashVarTest.swf
-swf-version 10
-main FlashVarTest
-swf-header 800:600:30:ffffff
version #9320, modified 2010-10-18 15:28:34 by shelby