LocalConnection Class – simple example

Very long ago I was contacted by Silvia Lifman asking me to provide an example using the LocalConnection Class where you'd write in one text field (in one SWF) and the other text field (in the other SWF) would display exactly the same text at the same time.

I created a very simple example that does this, based on the code already available in Macromedia's Livedocs.

receiving SWF:

Actionscript:
  1. // Code in the receiving SWF file
  2. // create the dynamic text field
  3. this.createTextField("result_txt", 1, 10, 10, 100, 22);
  4. result_txt.border = true;
  5. // create the local connection in the receiving file
  6. var receiving_lc:LocalConnection = new LocalConnection();
  7. // define the function that is going to be triggered by the other movie (sending file)
  8. receiving_lc.methodToExecute = function(t:String)
  9. {
  10.     // write something in this text field
  11.     result_txt.text = t;
  12. };
  13. receiving_lc.connect("lc_name");

sending SWF:

Actionscript:
  1. // Code in the sending SWF file
  2. var sending_lc:LocalConnection = new LocalConnection();
  3. /*
  4.     create an input text field
  5. */
  6. this.createTextField("text_txt", 1, 10, 10, 100, 22);
  7. text_txt.type = "input";
  8. text_txt.border = true;
  9. // when the text changes, i.e., when you write something
  10. text_txt.onChanged = function()
  11. {
  12.     // send the text to the other text field by calling the function "methodToExecute" and sending the content of the text field as a parameter
  13.     sending_lc.send("lc_name", "methodToExecute", this.text);
  14. };

You can download the files here.

I've also added my example to the Livedocs as I believe it's a bit better than the existing one. Wouldn't mind seeing it in included in 8-Ball's documentation!

About nuno mira

flash developer
This entry was posted in 1. Bookmark the permalink.

Comments are closed.