0

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.

6
  • are the two swfs on the same page? Commented Jan 22, 2010 at 22:28
  • No they belong to two different pages Commented Jan 22, 2010 at 22:30
  • my idea is to load the other page and then call the function I want to call....I can load the other page...but then I don't know how to call the function... Commented Jan 22, 2010 at 22:31
  • I should have been more clear. Are these being viewed in an internet browser? If so, do both swfs appear on the same web-page? Or, are you loading one page (say page1.html) and you want to call a function in a swf located on a separate page (page2.html)? Commented Jan 22, 2010 at 22:50
  • I explain, I have a page1.html (made in flash) if I push a button I wanna open page2.html (made in flash) and call a function belonging to this second one. Hope I made it clear. thanks in advance Commented Jan 22, 2010 at 22:59

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is awesome. This was the "missing piece" I needed in order to implement a "connect with Facebook" workflow from within a Flash object...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.