Page 1 of 1

Using FlashVars in AS2 and AS3 Rate Topic: -----

#1 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Posted 19 February 2009 - 04:58 AM

FlashVars are a technique for passing data to Flash in the form of name/value pairs. These are passed through the object tag of the web page, either as a parameter or as GET variables appended to the swf filename. The following examples illustrate these different techniques:

Example 1 - as a parameter
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="200" height="400" id="myflash">
	<param name="movie" value="mymovie.swf" />
	<param name="FlashVars" value="name1=value1&name2=value2" />
	<param name="quality" value="high" />
	<param name="bgcolor" value="#000000" />
	<embed src="mymovie.swf" FlashVars="name1=value1&name2=value2" quality="high" bgcolor="#000000" width="200" height="200" name="myflash" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />
</object>



Example 2 - appended to the filename
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="200" height="400" id="myflash">
	<param name="movie" value="mymovie.swf?name1=value1&name2=value2" />
	<param name="quality" value="high" />
	<param name="bgcolor" value="#000000" />
	<embed src="mymovie.swf?name1=value1&name2=value2" quality="high" bgcolor="#000000" width="200" height="200" name="myflash" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />
</object>



Note that in both these examples the variables are declared twice, once in a param tag and once in the embed tag. This is because different browsers use different parts of the object tag. Internet Explorer reads the object and param tags, whilst Netscape compatible browsers read the embed tag.


Using these variables in your actionscript code is done slightly differently depending on the version of actionscript you are using. In AS1 and AS2, using FlashVars is very simple. The variables are all made available within the root scope of your movie, and so are accessed like this:

trace(_root.name1);
//outputs: value1



or, if you prefer:

trace(_level0.name1);
//outputs: value1




In AS3, things are done slightly differently. The FlashVars are now stored as part of the loaderInfo object, and are accessed like this:

trace(root.loaderInfo.parameters.name1);
//outputs: value1



That's all for FlashVars, have fun with them!

Is This A Good Question/Topic? 0
  • +

Replies To: Using FlashVars in AS2 and AS3

#2 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Posted 29 April 2009 - 07:11 AM

It has come to my attention that not all AS3 classes can access root, and hence the flashVars. The way I'm getting around this right now is to access the loaderInfo object from the class that is directly associated with my document, and put them as statics into a singleton that can be accessed from any class in the application.
Was This Post Helpful? 0
  • +
  • -

#3 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Posted 13 September 2011 - 03:03 AM

To further clarify the above, "root" is a property of DisplayObject and it's ancestors, but it's value is not set until the object has been added to the stage. Solutions are to either store the loaderInfo parameters object earlier in the application flow, or use a reference to an object that is currently on the stage.

This post has been edited by thehat: 13 September 2011 - 03:19 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1