loadVariables()… Nooooo!
Lets face it, when you are forced to refer to externally loaded variables via _root, it just screams unefficiency. While I try not to be obsessive while developing anything with flash, depending on the situation many times you will find that with flash you do need to cut corners sometimes, but cutting corners does not nessecarily mean cutting efficiency.
When you use loadVariables() or loadVariablesNum() it sets whatever variables are set in the output of the URL to _root. Sure, you can specify the target movieclip or level to load these variables, but isnt that sooo flash 5 or below?
Although it is a bit late in the game to even mention the LoadVars object, I still to this day meet other flashers who still use loadVariables(). The first misconception of the LoadVars object is that it complicates things far more than what is nessecary.
Lets go over a bit as to how you can replace loadVariables() and loadVariablesNum() with the LoadVars object.
Lets take for example this line of code:
-
loadVariables("http://www.mywebsite.com/myscript.php", _root);
This will load whatever variables are set in the textual output of the url (example: &variable=value). Sure, you dont have to refer to _root, infact you could target a movie clip. But then a new problem presents itself: good luck detecting when its done loading! You could use onEnterFrame to check when a particular variable is set, but that is nonsense! Here i will show you how you can replace the above code with with LoadVars object.
-
var myLoadVars:LoadVars = new LoadVars();
-
myLoadVars.onLoad = function(success:Boolean)
-
{
-
if(success)
-
{
-
trace("SUCCESS");
-
trace("Variable set from text output of the URL:" + this.outputVariable);
-
}
-
else
-
{
-
trace("FAILURE");
-
}
-
};
-
myLoadVars.variable1 = "VALUE!";
-
myLoadVars.sendAndLoad("http://www.mywebsite.com/myscript.php", myLoadVars, "GET");
Now thats it! I won't play the role of flash's help documentation so this is all I got for ya for now! The variables recieved back from the server is yours to do what you want with it now. I suggest using a static class to store variables that need to be multi-level, instead of using _global or _root or _parent. I'll write more about that some other time though.
And as I did mention before I am still working on a few neat WP plugins, stay tuned for that!

Leave a Reply