AS2 AS3 crosstalk

AS2 AS3 crosstalk

You still have AS2 movies, which require a lot of work to convert. However, you want to make use of the huge advantages of Actionscript 3. The problem: You want to send variable values from the AS2 movie to the AS3 movie. While you can use the LocalConnection class to do it in the iopposite way (AS3 --> AS2) this is not possible for AS2 --> AS3. The only possibility is to use server-side scripts or javascript. The following shows how to accomplish that with Javascript.

To see the javascripts view source of this html page.

AS2 movie

This example has been modified from the ExternalInterface example published on the Adobe web site. The following script is for a button component in the AS2 movie. Data are sent to the formSend() metod in the HTML page, which is a javascript method with the parameter "jsArgument".



import flash.external.ExternalInterface;
ExternalInterface.addCallback ("sendTextToFlash",this,getTextFromJavaScript);
function clickSend (eventObj:Object):Void
{
	var jsArgument:String = sending_ti.text;
	var result:Object = ExternalInterface.call ("formSend", jsArgument);
}
send_button.addEventListener ("click",clickSend);
		
		

AS3 movie

In the AS3 movie we call also a javascript method to receive the data. The second script is to load the AS2 movie.



import flash.external.ExternalInterface;
function getTextFromJavaScript (str:String):void
{
	received_ti.text = str;
}
ExternalInterface.addCallback ("sendTextToFlash", getTextFromJavaScript);
var myLoader=new Loader();
myLoader.load (new URLRequest("Sender.swf"));
addChild (myLoader);