School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,000 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,977 people online right now. Registration is fast and FREE... Join Now!




Using FlashVars in AS2 and AS3

 
Reply to this topicStart new topic

> Using FlashVars in AS2 and AS3

thehat
Group Icon



post 19 Feb, 2009 - 03:58 AM
Post #1


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
CODE

<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
CODE

<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:

as

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


or, if you prefer:

as

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:

as

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


That's all for FlashVars, have fun with them!
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

thehat
Group Icon



post 29 Apr, 2009 - 06:11 AM
Post #2
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.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 06:15AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month