Here is a very interesting subject, not that the others seen so far aren"t, but this time we make Flash CS3 communicate with the server.
What"s changed in order to send/receive variables from server side scripts are quite a few.
First of all, the AS2 LoadVars class has been removed and is not longer used by AS3.
Here is an example on how to behave with Actionscript 3.0"
I create an FLA and save it as " load_vars.fla " . I create the Document Class, an AS file that I save as " ExLoadVars.as " , like so:
Code:
package
{
import flash.display.MovieClip;
import flash.net.URLLoader;
import flash.net.URLVariables;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.text.TextField;
import flash.display.SimpleButton;
import flash.events.*;
public class ExLoadVars extends MovieClip
{
public function ExLoadVars()
{
addButtonListener();
}
private function addButtonListener():void
{
board_mc.invia_btn.addEventListener(MouseEvent.MOUSE_DOWN,inviaDati);
}
private function addListeners(d:IEventDispatcher):void
{
d.addEventListener(Event.OPEN,inizio);
d.addEventListener(ProgressEvent.PROGRESS,inProgresso);
d.addEventListener(Event.COMPLETE,completato);
d.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityError);
d.addEventListener(HTTPStatusEvent.HTTP_STATUS,httpStatus);
d.addEventListener(IOErrorEvent.IO_ERROR,ioError);
}
private function inizio(e:Event):void
{
debug_txt.appendText('script chiamato: duplicanumero.php\n');
debug_txt.appendText('inizio: \n');
}
private function inProgresso(e:ProgressEvent):void
{
debug_txt.appendText('percentuale caricata: '+e.bytesLoaded+' totale: '+e.bytesTotal+'\n');
}
private function completato(e:Event):void
{
var loader:URLLoader=URLLoader(e.target);
debug_txt.appendText('completato: '+loader.data+'\n');
var vars:URLVariables=new URLVariables(loader.data);
board_mc.answer_txt.text=(vars.answer).toString();
debug_txt.appendText('La risposta è '+vars.answer+'\n');
}
private function securityError(e:SecurityErrorEvent):void
{
debug_txt.appendText('errore sicurezza: '+e+'\n');
}
private function httpStatus(e:HTTPStatusEvent):void
{
debug_txt.appendText('errore HTTP: '+e+'\n');
}
private function ioError(e:IOErrorEvent):void
{
debug_txt.appendText('Errore in invio/caricamento: '+e+'\n');
}
private function inviaDati(m:MouseEvent):void
{
debug_txt.text='';
var NumeroInserito:Number=Number(board_mc.insert_txt.text);
var variables:URLVariables=new URLVariables('numero='+NumeroInserito);
var richiesta:URLRequest=new URLRequest();
richiesta.url='http://www.flepstudio.org/swf/duplicanumero.php';
richiesta.method=URLRequestMethod.POST;
richiesta.data=variables;
var loader:URLLoader=new URLLoader();
loader.dataFormat=URLLoaderDataFormat.VARIABLES;
addListeners(loader);
try
{
loader.load(richiesta);
}
catch (error:Error)
{
trace('impossibile caricare la richiesta');
}
}
}
}
Try my example here:
The PHP script called (duplicanumero.php) is very simple:
PHP Code:
<"php
$num=$_POST["numero"];
$answer=$num*2;
echo ("answer=".$answer);
">
Let"s analyze Actionscript:
The three functions ( methods ) worth commenting on are inviaDati(), addListeners() and completato() .
InviaDati
With AS2 we were used to initiate the LoadVars class, associate some properties to it and send it via POST or GET.
As already mentioned, the LoadVars class has been removed, so here is the way to obtain the same result ( much more straightforward and logical than AS2, as it shows exactly what happens ):
I create a variable to hold the value I want to send to the PHP script
var NumeroInserito:Number=Number(board_mc.insert_txt.t ext);
I initialize the URL Variables class and I pass the above variable to it:
var variables:URLVariables=new URLVariables('numero='+NumeroInserito);
I initialize the URLRequest class to " prepare " Flash " that is about to send data via http
var richiesta:URLRequest=new URLRequest();
I define the request"s URL
richiesta.url='http://www.flepstudio.org/swf/duplicanumero.php';
I specify the sending method (POST in this case)
richiesta.method=URLRequestMethod.POST;
I assign the value to send contained in the variables variable to the .data property of the richiesta variable
richiesta.data=variables;
initialize the URLLoader class
var loader:URLLoader=new URLLoader();
I tell loader to format the string for the data transfer ( as if it were "numero=")
loader.dataFormat=URLLoaderDataFormat.VARIABLES;
I add the necessary listeners
addListeners(loader);
and I call the PHP script
try
{
loader.load(richiesta);
}
catch (error:Error)
{
trace('impossible to load the request');
}
addListeners
nothing new in this method of the ExLoadVars class; I just add the listeners for each event I need to monitor during the call to the PHP script
completato
This method is executed when Flash has completed the data transfer, since a listener is listening ( addListeners ), as we process the data returned by PHP, like so:
I initialize the URLLoader class passing the event"s .target property to it
var loader:URLLoader=URLLoader(e.target);
I initialize the URLVariables class passing the loader"s .data property to it
var vars:URLVariables=new URLVariables(loader.data);
I load the value returned by the PHP script into the text field, creating a vars" property with the same name as returned by the PHP script
board_mc.answer_txt.text=(vars.answer).toString();
Nice uhu " ;)
Source files: