从另一个Flash脚本调用flash脚本的函数

时间:2010-01-22 22:19:17

标签: flash

您好我是Flash的新手,我正在尝试执行以下操作:

在一个flash页面脚本中我想调用属于另一个flash脚本的函数,怎么做?

提前谢谢。

1 个答案:

答案 0 :(得分:3)

您可以使用LocalConnection实现此目的。

接收SWF:

private var localConnection:LocalConnection
//inside constructor
{
  localConnection = new LocalConnection();
  //in case both SWF's are in different domains
  localConnection.allowDomain("sender.swf's.domain");
  //in case receiver is in HTTPS and sender is non-HTTPS
  localConnection.allowInsecureDomain("sender.swf's.domain");
  localConnection.connect("connectionName");
  localConnection.client = this;

  //initialize TextField (tf) here
}
public function writeMsg(msg:String):void
{
  tf.text += "\nReceived message\n" + msg;
}    

发送SWF:

private var localConnection:LocalConnection;
private var connectionName:String;
//inside the constructor
{
  connectionName = "connectionName"; //use the same name as in receiver 
  localConnection = new LocalConnection();
  localConnection.addEventListener(StatusEvent.STATUS, onStatus);
  localConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);
}

//to call the "writeMsg" function in the receiver SWF from sender SWF
localConnection.send(connectionName, "writeMsg", "It works!");

private function onStatus(e:StatusEvent):void
{
  trace("statusEventHandler: code = " + e.code + ", level = " + e.level);
}
private function onSecError(e:SecurityErrorEvent):void
{
  trace("unable to make LocalConnection due to Security Error: " + e.text);
}

请记住,本地连接是单一的 - 通信是一种方式。对于双向通信,您必须设置另一对本地连接,并使用不同的连接名称从相应的SWF中调用connect