How to use FlashVars in Flash CS3 and Actionscript 3
September 6th, 2007
In Actionscript 3 you have to do a little more work then AS2.
When working in AS2 you could easily use a random object:
[as]
_flashVar = flashVar
//rest of code
[/as]
With AS3 you could run into IOERRORs and everything in between. The client app could end up with the ugly Runtime Error box, so being proactive about it is better.
Cause you never know, it may work great in your perfect world but when the PHP guy builds the XML structure wrong or the production artist forgets to upload the pictures. All the client will see is you dropped the ball on the flash.
So if you want to use FlashVars in AS3 I’ve found this to be a good solution.
You can compile this script out right or use Flash CS3 (that’s why it extends MovieClip)
[as]
package {
//lazy way to import MovieClip and future display package classes
import flash.display.*;
//lazy way to import Event.COMPLETE, IOErrorEvent.IO_ERROR,
//SecurityErrorEvent.SECURITY_ERROR and future event package classes
import flash.events.*;
import flash.text.TextField;
//extends movie clip to be compatible with flash
public class FlashVarsExample extends MovieClip
{
public function FlashVarsExample()
{
//the application’s flashvars is typed as a LoaderInfo object
// ready to do stuff!
var xmlData:LoaderInfo = root.loaderInfo as LoaderInfo;
// now xmlData listen for many event
// I pushed this to a method idea I got from an Adobe Tutorial
// It is great cause you can centralized all your events
// a re-add listeners to multiple objects
addListeners(xmlData);
}
//if domain error
private function securityErrorHandler(event:SecurityErrorEvent):void {
//declare local variable, it allows me to apply to many objects
// what ever object is listening will be the currentTarget
// makes the code more flexible instead making lots of event handler
//of a private _xmlData variable
//Thought there would never bee another set of FlashVars
var xmlData:LoaderInfo = LoaderInfo(event.currentTarget)
//clean up all the listener
removeListeners(xmlData)
xmlData = null;
trace(”securityErrorHandler: ” + event);
}
//if flashvars do not exist
private function ioErrorHandler(event:IOErrorEvent):void {
var xmlData:LoaderInfo = LoaderInfo(event.currentTarget)
removeListeners(xmlData)
xmlData = null;
trace(”ioErrorHandler: ” + event);
}
//when flashvars are loaded
private function loadFlashVars(evt:Event):void
{
trace(”loaded: ” + evt);
var xmlData:LoaderInfo = LoaderInfo(evt.currentTarget)
// if there is a property w/ xmlData load that as a string into loadXML
// if not loadXML will say “no dice”
var loadXML:String = (xmlData.parameters.hasOwnProperty(”xmlData”)) ? xmlData.parameters.xmlData as String: “no dice”;
removeListeners(xmlData)
xmlData = null;
trace(xmlData);
trace(loadXML);
// create a text field and display loadXML
var _txt:TextField = new TextField()
_txt.background = true;
_txt.text = loadXML;
addChild(_txt);
}
private function addListeners(dispatch:EventDispatcher):void
{
//when the data from the flashvars is now loaded call loadFlashVars method
dispatch.addEventListener(Event.COMPLETE, loadFlashVars);
//when there is no flashvars call loadFlashVars ** prevents the ugly runtime error box **
dispatch.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
//when data is been loaded from an untrusted domain ** prevents the ugly runtime error box **
dispatch.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
}
private function removeListeners(dispatch:EventDispatcher):void
{
// clean up after yourself you don’t need wasted memory
// listening for some thing that already happend and will not happen again
dispatch.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
dispatch.removeEventListener(Event.COMPLETE, loadFlashVars);
dispatch.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
}
}
}
}
[/as]
All you need to do now is make a php, html, jsp or whatever that has a flashvar with the name xmlData.
Hopefully this helped someone who doesn’t want to you object as a DataType for every variable or nasty error pop-ups.
Next we will actually load some XML
Last 5 posts in actionscript
- HOWTO Create a Facebook App using FlexBuilder - June 24th, 2009
- Roman Numeral/Arabic Convertion AIR App - Johnny V Now Available - December 25th, 2008
- How to convert Roman Numerals and Arabic numbers in ActionScript 3 - December 17th, 2008
- Using Flash Player 10 to produce Dynamic Musical Notes - May 20th, 2008
- Flash Player 10 Dynamic Sound Generation - May 20th, 2008
Last 5 posts in as3
- HOWTO Create a Facebook App using FlexBuilder - June 24th, 2009
- Guitar Synth for Flash - February 27th, 2009
- Abstract Thermometer AIR app - January 22nd, 2009
- Roman Numeral/Arabic Convertion AIR App - Johnny V Now Available - December 25th, 2008
- How to convert Roman Numerals and Arabic numbers in ActionScript 3 - December 17th, 2008
Last 5 posts in flash
- HOWTO Create a Facebook App using FlexBuilder - June 24th, 2009
- Roman Numeral/Arabic Convertion AIR App - Johnny V Now Available - December 25th, 2008
- How to convert Roman Numerals and Arabic numbers in ActionScript 3 - December 17th, 2008
- Flash Player 10 Pure Flash Keyboard using SampleDataEvent - September 3rd, 2008
- Using Flash Player 10 to produce Dynamic Musical Notes - May 20th, 2008
Entry Filed under: actionscript, as3, flash
4 Comments Add your own
1. Rumkin | March 7th, 2008 at 6:19 pm
Hi, very nice. Big thanks you helps me a lot;
2. Gek Hua | March 25th, 2008 at 9:54 am
i am a bit confused after reading the code … i thought addListeners (line 21) is out of AS3?
3. joshspoon | March 26th, 2008 at 6:12 pm
if you look at line 80 you’ll see it’s calling an internal function.
4. Vincent Lessard | September 10th, 2009 at 2:32 pm
Yah, really nice code ! It works perfect !!!
The code is well commented either
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
Trackback this post | Subscribe to the comments via RSS Feed