tags:

views:

22

answers:

1

Hello I'm a newbie of Flash, I'm trying to do the following:

in one flash page script I want to call a function belonging to another flash script, how to do that?

Thank you in advance.

+1  A: 

You can achieve this using LocalConnection.

Receiving 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;
}    

Sending 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);
}

Remember that local connections are simplex - the communications are one way. For two-way communication, you have to set up another pair of local connections and call connect from the appropriate SWF with a different connection name.

Amarghosh