AS2 → AS3: LoadVars AS3 Equivalent
ActionScript 3 has done away with the LoadVars class and when I was updating my ContactForm for AS3 I was trying to figure out how to mimic the sendAndLoad() method that LoadVars provided. I stumbled upon an article by Peter Elst which explained how to do this so I'm going to outline the differences here.
Let's take a look at the old AS2 code:
-
var msg:LoadVars = new LoadVars();
-
var msgSent:LoadVars = new LoadVars();
-
-
msg.var1 = "one";
-
msg.var2 = "two";
-
-
msgSent.onLoad = function($success:Boolean):Void
-
{
-
if ($success)
-
{
-
trace("Message sent.");
-
}
-
else
-
{
-
trace("Message failed.");
-
}
-
};
-
-
msg.sendAndLoad("http://www.reintroducing.com/script.php", msgSent);
And here is the AS3 equivalent:
-
var scriptRequest:URLRequest = new URLRequest("http://www.reintroducing.com/script.php");
-
var scriptLoader:URLLoader = new URLLoader();
-
var scriptVars:URLVariables = new URLVariables();
-
-
scriptLoader.addEventListener(Event.COMPLETE, handleLoadSuccessful);
-
scriptLoader.addEventListener(IOErrorEvent.IO_ERROR, handleLoadError);
-
-
scriptVars.var1 = "one";
-
scriptVars.var2 = "two";
-
-
scriptRequest.method = URLRequestMethod.POST;
-
scriptRequest.data = scriptVars;
-
-
scriptLoader.load(scriptRequest);
-
-
function handleLoadSuccessful($evt:Event):void
-
{
-
trace("Message sent.");
-
}
-
-
function handleLoadError($evt:IOErrorEvent):void
-
{
-
trace("Message failed.");
-
}
As you can see, there is a new URLVariables class that stores the information you want to pass to your script. You then pass that URLVariables instance to the URLRequest's data property. Essentially, what this does, is create a query string with all the variables appended to it. The above actually looks like this when it is sent to the server:
http://www.reintroducing.com/script.php?var1=one&var2=two
That is what the PHP script receives and then handles it on the script's end to do whatever it is that you are wanting to do. I also explicitly set the URLRequest's method to be POST by setting the URLRequestMethod.POST constant (for GET, you'd just set URLRequestMethod.GET).
If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.
48 Comments
[...] that sometimes what used to be accomplished in few steps now takes more. One of these is the old AS2 LoadVars object. It was pretty straight forward, but now it requires multiple classes and methods to make work. [...]
Maciej,
Can you read query string parameters with this URLVariables class. For instance in your example:
scriptVars.var1 = "one";
scriptVars.var2 = "two";
Say in the URL there was a querystring parameter that already exists http://www.dkdkdkdkd.com?var1=one
Can I do var strQueryStringValue:String = scriptVars.var1 and it store the value "one" in the string variable?
Richie,
Unfortunately it's not as easy as that. There has to be a javascript call to get the URL and then parse the URL in Flash. I'm gonna work on making a class to do this in AS3.
Thank you for just being you! You're the man, and I take back all the bad things I said about you. Ok, so maybe not all of them.
Seriously though, thanks man, this actually was a nice find and hooked up my problem nicely. Now that we can finally use AS3 (almost 3 years after it came out...go figure), it's been a serious headache trying to handle some of the simplest things that are now different. I feel like a n00b again....it's awesome!
haha, you're welcome jason, although the game i just did for you guys was still in AS2
glad this helped.
This is exactly what I was looking for. Having to re-write a bunch of tracking code into AS3. Feel like I'm back at square one again.... until I came across your wee gem!
Matt, have a question for you:
My Flash is embedded in a .ASP page that receives a query string as part of the URL, decodes it and passes it to the Flash through loadVars. How would I change your code example to reflect this?
Thanks for any and all advice you can give me...
@Patrick: you don't need this for what you are doing. what you need is pulling in FlashVars which you can find examples of (both AS2 and AS3) here: http://evolve.reintroducing.com/2008/03/21/as2-to-as3/as2-%E2%86%92-as3-retrieving-flashvars/
hope that helps!
Hi I get an error message when I run the full script!
Description:
"1046: Type was not found or was not a compile-time constant: Event.
Source:
"function handleLoadSuccessful($evt:Event):void
But if I leave out the eventlisteners and the functions it works, but then I'm not able to tell if if succeded in flash.
I'm new to AS 2 or 3 - but in the AS2 script - any information returned by the PHP file will end up in msgSent - example, if I have a $imageName = "image.jpg", then shouldn't I refer to that back in AS2 script as msgSent.imageName ?
If so - how is that being done in your AS3 version? If it isn't, then how would that be done in AS3
Thanks in advance, and thank you for this script.
HI been using this for a while, I have run into a weird situation where the script works fine in firefox but fails in internet explorer. It only happens with a specific php script I am running. Any ideas what might be causing this?
@Shaun: If its working normally but only fails with a specific PHP script, then I'd have to venture and say that its the PHP scripts fault in which case I can't really help you since I don't know very much PHP :\
Very useful. Although once again, I am forced to step back from both code examples, and see the beautiful simplicity of AS2, and the confusing "wordiness" of AS3.
How this is an "improvement", I have no idea.
But thank you for the great tip.
Im try somthing like this :
function handleLoadSuccessful($evt:Event):void
{
Text2.text=scriptRequest.data("Url1")
}
But is not working
[...] اونو هم برای اکشن ۲ و هم برای اکشن ۳ نوشته و اینم آدرسش : http://evolve.reintroducing.com/2008…s3-equivalent/ و منم با آموزش خودم سعی می کنم دیگه این مسئله رو در این [...]
What about a result? The old AS2 sendAndLoad, LoadVars provided for the prospect of a return from the script with a "result.onLoad" method. How is that handled with AS3?
@John: The Event.COMPLETE acts the same as the onLoad method in AS2 did and you can pull a data value in the event parameter.
If anyone is having problems working out how to get variables back from this method, try the following.
Insert this line before the .load
scriptLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
This tell it that we are expecting url encoded variables back
Then in the handleLoadSuccessful function you can access the variableslike this:
scriptLoader.data.returnVar1
Where returnVar1 is an example variable name.
More info on the flash help pages
http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/net/URLLoader.html
Hi Matt,
Going back to your comment dated July 3, 2008 in response to Richay, did you ever get that class built and might you have a full sample including the required javascript?
Thanks for everything.
@Keith: I do have a class to do that now and can send it to you if you can provide your e-mail. It relies on some other classes to work so I'll have to package it all up for you.
Hi Guys,
I'm verry happy I came across this page.
I've been trying to implement your script to the button on my contact form page and unfortunately I can't get it to work.
Here's my code I have placed on the timeline of my fla:
var scriptRequest:URLRequest = new URLRequest("php/email.php");
var scriptLoader:URLLoader = new URLLoader();
var scriptVars:URLVariables = new URLVariables();
Submit.addEventListener(MouseEvent.CLICK, submitForm);
//Submit is my button
function submitForm(event:MouseEvent):void {
scriptLoader.addEventListener(Event.COMPLETE, scriptOk);
scriptLoader.addEventListener(IOErrorEvent.IO_ERROR, scriptError);
scriptVars.theName = contactName.text;
scriptRequest.method = URLRequestMethod.POST;
scriptRequest.data = scriptVars;
scriptLoader.load(scriptRequest);
}
function scriptOk($evt:Event):void {
MovieClip(parent).gotoAndPlay(2);
}
function scriptError($evt:IOErrorEvent):void {
MovieClip(parent).gotoAndPlay(2);
}
Can somebody advise why it's not working?
Thanks
Well sorry...
I should be more specific...
The script is working, it sends email however in my php file where i use $theName I can't see any data...
It looks like the above script doesn't send the data to php file.
Thanks!
Matt, thx for your reply. I have figured that out. There was a silly error with paths to objects. Thanks
Excelent e Funciona Perfecto, aca les dejo como pasar variables al insert, etcc.... http://msdn.microsoft.com/en-us/library/cc793139%28v=sql.90%29.aspx













thanks a lot! useful.