flashvarscatWhen building utilities that can be used with both Flash and Flex, there are two common hurdles that developers run into. The first one is that Flex does not pull FlashVars the same way that Flash does. This means for ActionScript utilities that rely on html inserted variables will need to be separated into two separate files or need to have flashvars passed into the utility from the outside. Another hurdle is that Flash does not have a global reference to Stage- instead it can only be accessed through display children of stage. This is problematic for utilities that work with Stage as they can only get there reference of stage when it’s manually passed into the utility as a parameter which can result in an ugly web of stage references being passed around.

However, I finally got the time to develop a workaround that solves both problems with classes FlashVars and StageReference. The tricky part of this implementation was building a utility that could use the class Application but not reference it when it was being built from Flash since it would cause the compiler to fail from the invalid class reference. I worked around this issue by grabbing Application by getDefinitionByName so that the Flash compiler wouldn’t fail since it was a weak reference.

With Flash, use StageReference.stage = stage in Frame 1 or in your document class’s constructor. This will give you the global reference to stage and also allow the new class FlashVars to pull flashvars automatically when compiled in a Flash project.

The custom class FlashVars is used in this fashion:

var myFlashvar:String = FlashVars.parameters["myFlashvar"];


var myFlashvar:String = FlashVars.parameters.myFlashvar;

This usage is exactly the same from Flash and Flex! No more having to switch from loaderInfo.parameters to Application.application.parameters when porting utilities between Flash and Flex.

Download (includes StageReference): flashvars.zip

Icon spoofed from icanhascheezburger.com.

Author: Jonathan Dunlap
Jonathan is a veteran software architect, author of IsoHill, humanitarian, and has worked with Bigpoint Inc, CrowdStar, ePrize, and Microsoft.
Be Sociable, Share!