Flex Snippet - Using FlashVars

Skill

Flex Snippet - Using FlashVars

Posted in:

One of the many ways of getting information in Flash and/or Flex is using FlashVars which can be passed in through the embedding mechanism. This method is used for many different things. It can be used to pass in a video to play or a simple parameter you don't want to define at compile time. You can see examples of this all over the web in places like Google Finance and YouTube.

Below you can see a small example of an application pulling in information using FlashVars. The color that is displayed is grabbed from the vars. You can also check out the FlashVars Demo Source if you would like. I am going to go over the pieces needed for this just after the demo.

Get Adobe Flash player

So really it is pretty simple to use FlashVars in Flex, the only thing you need to ever remember is Application.application.parameters the parameters object holds all the key value pairs for the vars coming in. In the demo above I am using the following code to get the favorite color.

favColor = Application.application.parameters.favColor;

That is it. Now the other piece we need to handle this is actually setting the FlashVars in the embed code. If you use swfobject or some other javascript mechanism to add the swf you will have to look in their documentation for adding extra parameters. These are usually very straight forward. As for doing this in the object and embed html tag, well I have an example below.

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
     id="FlashVars" width="300" height="150"
     codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
  <param name="movie" value="CoolApp.swf" />
  <param name="quality" value="high" />
  <param name="bgcolor" value="#869ca7" />
  <param name="allowScriptAccess" value="sameDomain" />
  <param name="flashvars" value="favColor=Green" />
  <embed src="CoolApp.swf" quality="high" bgcolor="#869ca7"
     width="300" height="150" name="FlashVars" align="middle"
     play="true"
     loop="false"
     quality="high"
     allowScriptAccess="sameDomain"
     type="application/x-shockwave-flash"
     pluginspage="http://www.adobe.com/go/getflashplayer"
     flashvars="favColor=Green" >
  </embed>
</object>

As you can see above we have added the new param and the object tag and a new attribute in the embed tag. And with that we are done with this short little tutorial, as always feel free to ask questions and voice any other comments. Until next time keep coding and playing video games (they are good for the soul).

Daman
11/19/2008 - 00:27

nice collection of c sharp topics.
thanks for these great topics discussions.

reply

Nash
06/04/2009 - 13:08

Few more ways to pass variable to flex

http://yasob.blogspot.com/2009/05/accessing-http-request-parameter-in.html

reply

Anonymous
05/20/2010 - 22:51

good work

reply

Anonymou
06/18/2010 - 13:51

i used this code but the output is null would u know why???

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.

Sponsors