In Actionscript 3 you have to do a little more work then AS2.
When working in AS2 you could easily use a random object:
-
_flashVar = flashVar
-
-
//rest of code
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)
-
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);
-
}
-
-
}
-
}
-
}
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
- 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
- How to compile and examples for Flash Player 10 or ASTRO BETA - May 16th, 2008
Last 5 posts in as3
- 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
- Flash Player 10 Dynamic Sound Generation - May 20th, 2008
Last 5 posts in flash
- 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
- Flash Player 10 Dynamic Sound Generation - May 20th, 2008
Josh Weatherspoon, A self-proclaimed millionaire who for some reason has to work as a Flash Developer, at Travelocity in Dallas(Southlake), TX.
Horaayy..there are 3 comment(s) for me so far ;)
Hi, very nice. Big thanks you helps me a lot;
i am a bit confused after reading the code ... i thought addListeners (line 21) is out of AS3?
if you look at line 80 you'll see it's calling an internal function.