At present, flash in all aspects of the application of more and more wide, and flash is not only focus on their own brilliant effect, but also need to exchange data with external programs to achieve more powerful functions, with the advent of AS3, Flash and external interaction of the way is increasingly simple and rationalized. Talk about the interaction of AS3 and background data here today (choose AS3 PHP Architecture, other configurations in the same vein).
In AS3, the original Loadvars method has been discarded, instead of using a series of classes to achieve interaction with the background data, these classes are: Urlloader,urlloaderdataformat,urlstream,urlvariables.
Similar to the original Loadvars, the URLLoader class is a powerful tool for loading this, 2 data, xml,url data, and other information. Urlloader accepts a urlrequest as a constructed parameter, using the Load method to load the data. Urlloder can also be monitored, which is also loadvars different, a simple example is as follows:
1.//////////////////actionscript code//////////////////////////////////////////
2.//Declare a Urlloader
3. var loader:urlloader = new URLLoader ();
4.//Monitor the load completion event for several loads
5. Loader.addeventlistener (event.complete,loaded);
6.//Event corresponding function
7. Function loaded (e:event) {
8. Trace (Loader.data);
9.//Output:this data is from php!
10.}
11.//Load test.php
Loader.load (New URLRequest ("test.php"));
1.//////////////////test.php code/////////////////////////////////
2. 3.//Declare a variable and assign it to a string
4. $data = ' This data is form php! ';
5.//output this variable to flash
6. Echo $data;
7.?>
As can be seen from the above example, AS3 and background interaction is simple, but this is only a single interaction, if you want Flash to send data back to the background and return to the data how to do it? At this point you may be putting the code in this way:
1.//actionscript Code
2. var loader:urlloader = new URLLoader ();
3. Loader.data={message: "Hello im flash!"};
4. Loader.addeventlistener (event.complete,loaded);
5. Function loaded (e:event) {
6. Trace (Loader.data);
7.//Output:this is Flash say:
8.}
9. Loader.load (New URLRequest ("test.php"));
Look at the output code, what do you get? Yes, the information is not transferred to PHP, you are still trying to transfer the data in a loadvars way, but the AS3 is not the case, urlloader data is not initialized until the download is finished, when the data is not loaded, it is equal to NULL, that is, It contains only the data received, regardless of the data to be sent, how does the AS3 send the data back to the background? Use Urlvariables. Urlvariables allows you to transfer variables between flash and background programs, AS3 has sent and received data separation, and no longer like AS2 Loadvars, a class takes all, then this urlvariables how to send the data?
You may have searched all over the Urlloader document and found nothing related to it, yes Urlloader is not associated with urlvariables, but URLRequest is using it, URLRequest contains all the information of the entire HTTP request, So the data we want to send is here, URLRequest has a data property he accepts an object type parameter, and that's what we want to send. You may also notice that URLRequest also manages the way the HTTP request is sent, and its method property accepts a character argument, post, or get. OK, now let's see how they work:
1.//actionscript Code
2. var loader:urlloader = new URLLoader ();
3.//Declare a urlrequest
4. var url:urlrequest = new URLRequest ("test.php");
5.//Set transfer mode to post
6. Url.method = Urlrequestmethod.post;
7.//Declare a urlvariables
8. var values:urlvariables = new Urlvariables ();
9.//Set the information to be transferred
Ten. Values.message= "Hello im flash!";
Url.data = values;
Loader.addeventlistener (event.complete,loaded);
function loaded (e:event) {
. Trace (Loader.data);
//Output:this is Flash Say:hello im flash!
16.}
Loader.load (URL);
How, is not also very convenient to achieve the AS3 and the background of data transmission? Well, now our program can be two-way interactive data, but this is just some simple data, if you want to transfer the structure of the data, (people familiar with AS2 know that Loadvars can automatically parse the structure of the download data), let us first try to loadvars way, See if Urlloader can automatically parse the downloaded data.
1.//actionscript Code
2. var loader:urlloader = new URLLoader ();
3.//Declare a urlrequest
4. var url:urlrequest = new URLRequest ("test.php");
5.//Set transfer mode to post
6. Url.method = Urlrequestmethod.post;
7.//Declare a urlvariables
8. var values:urlvariables = new Urlvariables ();
9.//Set the information to be transferred
Ten. Values.message= "Hello im flash!";
Url.data = values;
Loader.addeventlistener (event.complete,loaded);
function loaded (e:event) {
. Trace (Loader.data);
//output:phpmessage=im php Message!&flashmessage=hello im flash!
. Trace (Loader.data.phpMessage);
.//Thorw Error;
Trace (loader.data.flashMessage);
.//Thorw Error;
20.}
Loader.load (URL);
Can see that the data has been completely downloaded, but it has not been parsed, but also thrown an exception, then is not Urlloader can not automatically parse the data? No, on the contrary Urlloader offers a variety of data parsing options for you to choose from. These data parsing methods are in the Urlloaderdataformat, they are: binary-in 2-way parsing text-in the form of text parsing variables-in the form of variable-value pairing resolution. Urlloader's DataFormat property provides a choice of parsing methods, so let's change the program to make it more perfect:
1.//actionscript Code
2. var loader:urlloader = new URLLoader ();
3.//Declare a urlrequest
4. var url:urlrequest = new URLRequest ("test.php");
5.//Set transfer mode to post
6. Url.method = Urlrequestmethod.post;
7.//Declare a urlvariables
8. var values:urlvariables = new Urlvariables ();
9.//Set the information to be transferred
Ten. Values.message= "Hello im flash!";
Url.data = values;
12.//Set to parse data in a variable-value pairing way
Loader.dataformat.
= Urlloaderdataformat.variables;
Loader.addeventlistener (event.complete,loaded);
function loaded (e:event) {
. Trace (Loader.data);
//output:phpmessage=im php Message!&flashmessage=hello im flash!
Trace (loader.data.phpMessage);
//im PHP message!
Trace (loader.data.flashMessage);
//Hello im flash!
22.}
Loader.load (URL);
These are the basics of AS3 and background interaction, and now you can create such interactions in AS3, get Dynamic Data to enrich your programs, and before you start your own labs, there are some things to note:
1, remember to capture and handle the exception (the above example is assumed to run in the perfect test environment, so there is no exception capture and processing code, but in reality you may encounter a variety of situations) Urlloader may cause several anomalies (see the documentation for details)
You should listen to IOError and Securityerror events, or deal with them through try-catch, even if you are sure that this problem will not happen.
2, the file encoding, this is a common problem, often a lot of data into garbled, or parse error, all because of the problem of coding, my practice is to ensure that the data sent and back to the background is UTF-8 format (because the flash default encoding is Utf-8)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.