FlashVars with Flex 2
The switch from Flash to Flex can sometimes be frustrating when trying to do the things that we took for granite in Flash. FlashVars were so easy because they were automatically available on the root timeline object. Well, in Flex they are still automatically made available to you, just not in a place that you might expect.
The name/value pairs from FlashVars are added to the dynamic object Application.application.parameters and the named variables can be accessed as parameters.name = value.
FlashVars can be tricky though, especially when they are dynamically populated in the HTML by server-side code or when grabbing url parameters and adding them to FlashVars. You are not always guaranteed that the name/value pair will always be present, so you'll need to deal with the possibility of an undefined value. The following example shows a great way to deal with this situation which was given to me by a friend at work, be sure to check out her blog for other great Flash development tips. Also for additional information on this subject see the article in the LiveDocs.
The following example assumes that there are FlashVars defined in the HTML similar to this:
The name/value pairs from FlashVars are added to the dynamic object Application.application.parameters and the named variables can be accessed as parameters.name = value.
FlashVars can be tricky though, especially when they are dynamically populated in the HTML by server-side code or when grabbing url parameters and adding them to FlashVars. You are not always guaranteed that the name/value pair will always be present, so you'll need to deal with the possibility of an undefined value. The following example shows a great way to deal with this situation which was given to me by a friend at work, be sure to check out her blog for other great Flash development tips. Also for additional information on this subject see the article in the LiveDocs.
The following example assumes that there are FlashVars defined in the HTML similar to this:
<param name=FlashVars value="uid=monkey">
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init();" viewSourceURL="srcview/index.html"> <mx:Script> <![CDATA[ public var userID:int; private function init():void { if ('uid' in Application.application.parameters) { userID = int(Application.application.parameters.uid); } else { trace("No user id was passed in!"); } } ]]> </mx:Script> </mx:Application>