26Jan FlashVars for EVERYONE!
When 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:
or
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.


January 30th, 2009 at 2:14 pm
[...] > FlashVars for EVERYONE! | JADBOX: Web Application Musings [...]
June 4th, 2009 at 11:11 am
Few more ways to pass variable to flex
http://yasob.blogspot.com/2009/05/accessing-http-request-parameter-in.html
June 19th, 2010 at 7:43 pm
[...] there is such thing. It’s called JadBox Flashvar class. You can get the util class here. Here an example how this class is used (remember to import the jadbox class into your main first [...]