How to use FlashVars in Flash CS3 and Actionscript 3

Sep
6

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

 

5 Responses to “How to use FlashVars in Flash CS3 and Actionscript 3”

  1. Rumkin says:

    Hi, very nice. Big thanks you helps me a lot;

  2. Gek Hua says:

    i am a bit confused after reading the code … i thought addListeners (line 21) is out of AS3?

  3. joshspoon says:

    if you look at line 80 you’ll see it’s calling an internal function.

  4. Yah, really nice code ! It works perfect !!!

    The code is well commented either ;)

  5. Phil says:

    How to you implement this code?
    I have saved it as a .as file and imported it into my flash application….
    I have my flashvars on my html page

    how do i make use of this code now?

Leave a Reply