[ActionScript 3.0] AS3.0和AS2.0的相互通信

时间:2022-10-06 16:43:52

AS3和AS2之间的通信,最好的方式可能就是LocalConnection了。

AS2向AS3发送数据,即AS2调用AS3的函数:

as2.0代码(按钮上写的发送信息代码):

on (release) {
var param = "this message is from as2";
var caller:LocalConnection = new LocalConnection();
caller.send("AS2 send to AS3","funInAS3",param);
//1、相互连接的标识 2、as3中定义的方法 3、需要传递的参数
}

as3.0代码(textAare为舞台上的文本框):

var server:LocalConnection = new LocalConnection();
server.addEventListener(StatusEvent.STATUS,function(event:StatusEvent):void{
trace(event.level);
});
server.connect("AS2 send to AS3");
server.client = this;
server.allowDomain("*");
server.allowInsecureDomain("*");
//接收as2.0发送的信息
function funInAS3(value:String):void
{
trace("AS3:",value);
textAare.appendText("AS3:"+value+"\n");
}

AS3向AS2发送数据,即AS3调用AS2的函数:

as3.0代码(sendBtn为发送信息按钮):

sendBtn.addEventListener(MouseEvent.CLICK,onSendToAS2);
function onSendToAS2(e:MouseEvent):void
{
var caller:LocalConnection = new LocalConnection();
var param:String = "this message is from as3";
   caller.send("AS3 send to AS2","funInAS2",param);
}

as2.0代码(_root.textArea为舞台上的文本框):

var server:LocalConnection = new LocalConnection();
server.connect("AS3 send to AS2");
server.allowDomain = function(sendingDomain)
{
return true;
}; //可以接收as3传递的值
server.funInAS2 = function(s:String)
{
trace(s);
_root.textArea.text += "AS2:"+s+"\n";
};