利用LocalConnection来通信。详见api中LocalConnection类
有4中通信方式
在一个swf文件内
在多个swf文件之间
在AIR应用程序的内容(基于SWF或基于HTML)之间
在 AIR 应用程序的内容(基于 SWF 或基于 HTML)和运行于浏览器中的 SWF 内容之间
同一个域。这是使用 LocalConnection 对象最简单的情况,它只允许在位于同一个域中的 LocalConnection 对象间通信,这是因为默认情况下,应用程序允许同域通信。当同一个域中的两个 文件通信时,无需实施任何特殊的安全措施,而只需将
connectionName
参数的同一个值传递给
connect()
和
send()
方法。
// receivingLC is in http://www.domain.com/receiving.swf
receivingLC.connect('myConnection');
// sendingLC is in http://www.domain.com/sending.swf
// myMethod() is defined in sending.swf
sendingLC.send('myConnection', 'myMethod');
具有可预知域名的不同域。当不同域中的两个 SWF 文件通信时,需要通过调用
allowDomain()
方法来允许在这两个不同域之间进行通信。还需要在
send()
方法中使用接收方 LocalConnection 对象的域名限定连接名:
// receivingLC is in http://www.domain.com/receiving.swf
receivingLC.allowDomain('www.anotherdomain.com');
receivingLC.connect('myConnection');
// sendingLC is in http://www.anotherdomain.com/sending.swf
sendingLC.send('www.domain.com:myConnection', 'myMethod');
具有不可预知域名的不同域。有时候,可能希望具有接收方 LocalConnection 对象的 文件在域之间具有更好的可移植性。为了避免在
send()
方法中指定域名,但要指出接收方和发送方 LocalConnection 对象不在同一个域中,可在
connect()
和
send()
调用中的连接名称之前加一个下划线 (_)。要允许在这两个不同域之间通信,请调用
allowDomain()
方法并传递您希望允许 LocalConnection 调用的域。或者,也可以传递通配符 (*) 参数来允许从所有域调用:
// receivingLC is in http://www.domain.com/receiving.swf
receivingLC.allowDomain('*');
receivingLC.connect('_myConnection');
// sendingLC is in http://www.anotherdomain.com/sending.swf
sendingLC.send('_myConnection', 'myMethod');
可以使用 LocalConnection 对象发送和接收单个 文件中的数据,但这不是通常的用法。
//====================我的实例
这是两个不同域的swf,要么用allowDomain('*');
connectionName前加下划线
要么用特定的域名限制,connectionName前不加下划线
ManagerApp.as
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.StatusEvent;
import flash.net.LocalConnection;
import flash.text.TextField;
import flash.text.TextFieldType;
public class ManagerApp extends Sprite
{
private var conn:LocalConnection;
private var _txtSend:TextField;
private var _btn:Sprite;
public function ManagerApp()
{
_txtSend = new TextField();
_txtSend.type = TextFieldType.INPUT;
_txtSend.text = "abc";
this.addChild(_txtSend);
_btn = new Sprite();
_btn.graphics.beginFill(0xff0000);
_btn.graphics.drawCircle(30,30,30);
_btn.graphics.endFill();
_btn.x = 100;
this.addChild(_btn);
_btn.addEventListener(MouseEvent.CLICK,sendMessage);
conn = new LocalConnection();
conn.addEventListener(StatusEvent.STATUS, onStatus);
}
private function sendMessage(e:MouseEvent):void
{
conn.send("app#ViewApp:connectionName","fun",_txtSend.text);
}
private function onStatus(e:StatusEvent):void{
switch(e.level)
{
case "status":
trace("LocalConnection.send() succeeded");
break;
case "error":
trace("LocalConnection.send() failed");
break;
}
}
}
}
ViewApp.as
package
{
import flash.display.Sprite;
import flash.net.LocalConnection;
import flash.text.TextField;
public class ViewApp extends Sprite
{
private var _txt:TextField;
private var conn:LocalConnection;
public function ViewApp()
{
_txt = new TextField();
_txt.text = "def";
this.addChild(_txt);
conn = new LocalConnection();
conn.allowDomain("app#ManagerApp");
conn.client = this;
try{
conn.connect("connectionName");
}catch(error:ArgumentError)
{
trace("Can't connect...the connection name is already being used by another SWF");
}
}
public function fun(str:String):void
{
_txt.text = "被调用:" + str;
}
}
}