Note: I’m converting my Flash game “Spin City” from AS2 to AS3 so it can be re-skinned for another project. I’ve decided to document this activity in a series of tutorials so they might benefit anyone else who is going through the same development process)
Sorry, I’ve been away from this for several weeks working on another project, but now I’m back converting AS2 to AS3. I’m going to restart this series again with a very simple, yet “$@#! frustrating” change from AS2 to AS3: flashvars.
Flash AS2 |
In AS2, passing variables from HTML into Flash was quite easy. You could do it one of two ways.
First, you could append the var to the .swf file reference in your tag of your HTML like this:
[cc lang=”javascript” width=”550″]
[/cc] |
Or you would add them to the flashvars parameter:
[cc lang=”javascript” width=”550″]
[/cc] |
To access these variables in .fla, you would target the same variable that was passed in (firstname, type ,etc) in _root , _level0, or just as an action on main timeline, and you would have instant access to those variables.
[cc lang=”javascript” width=”550″]var firstName = _root.firstname; var firstName = _level0.firstname; var firstName = firstname;[/cc] |
Flash AS3 |
In AS3, the path to access these variables has changed considerably. If your plan was to keep working with the same methods above, you would quickly find that the variables no longer exist on the main timeline.
First, you can no longer append the vars to the end of the .swf reference as in the first example above. You must use the flashvars parameter. Here is an example using Macromedia’s RunActiveContent script.
[cc lang=”javascript” width=”550″] if (AC_FL_RunContent == 0) { alert(“This page requires AC_RunActiveContent.js.”); } else { AC_FL_RunContent( (…) ‘flashvars’,’firstname=steve&ftype=nerd’ ); //end AC code }[/cc] |
Second, to get the variables back out, you use the new loaderInfo object that is available from the main timeline. To get the firstname passed in through flashvars, and display it in textbox you would do the following:
[cc lang=”javascript” width=”550″]<firstname.text = this.loaderInfo.parameters.firstname;
[/cc] |
If firstname is not set, you will get a “null reference error”, so some simple test such as the followng is helpful to get a .swf with a clean compilation:
[cc lang=”javascript” width=”550″]var testval:String = this.loaderInfo.parameters.firstname; if (testval != null) { firstname.text = testva; } else { firstname.text = “none”; } [/cc] |