Home Contact RSS

Local Connection Actionscript - Communicate between seperate Flash files | Tutorial

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5 out of 5)
Loading ... Loading ...

Overview:

Local Connection
Communication between two seperate flash files placed on the same page (or even running simultaneously on one machine) is a nice way to spread a project out. You can send variable, call functions, pretty much do anything in one swf from another. Easiest case use would be a navigation menu set up in one flash file to control the other one containing the content. I’ve made an example here showing how to send text from one to another. I’ve done it both directions here. Send text from the red swf to the blue swf, and from mr. Blue you send to the red flash file. I have named the flash functions in actionscript accordingly (or tried to, now I notice a few places I misspelled receive, ‘i’ before ‘e’, right? oh yea, except after ‘c’)…
Anyways, try out the example here, I made it a little easier by putting a keyListener on ‘Enter’, so you don’t have to actually press the send button. Didn’t realize it before, but this is like a chat app built in flash! So go ahead and chat with yourself to prove that it works!

Execute actionscript in one swf from another! Inter-swf communication.

Example:

Type here to send Red text to Blue flash file
(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

And see it received here, and go ahead and send some back to Red.
(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

Actionscript:

Red:

  1. // Receiving
  2. //create a local connection for reciept of text
  3. var receiving_lc:LocalConnection = new LocalConnection();
  4. //function called from other swf
  5. receiving_lc.recieveBlueText = function(textRecieved:String) {
  6. feedback.text += textRecieved+"\n";
  7. };
  8. //receive connection of specified name
  9. receiving_lc.connect("fromBlue");
  10.  
  11. //Sending
  12. sendButton.onRelease = function() {
  13. //create local connection for sending text
  14. var sending_lc:LocalConnection = new LocalConnection();
  15. //put text from input into a var
  16. var textToSend = inputText.text;
  17. //send through specified connection, call specified method, send specified parameter
  18. sending_lc.send("fromRed", "recieveRedText", textToSend);
  19. //set the input empty
  20. inputText.text = "";
  21. }

Blue:

  1. // Receiving
  2. var receiving_lc:LocalConnection = new LocalConnection();
  3. receiving_lc.recieveRedText = function(textRecieved:String) {
  4. feedback.text += textRecieved+"\n";
  5. };
  6. receiving_lc.connect("fromRed");
  7.  
  8. //Sending
  9. sendButton.onRelease = function() {
  10. var sending_lc:LocalConnection = new LocalConnection();
  11. var textToSend = inputText.text;
  12. sending_lc.send("fromBlue", "recieveBlueText", textToSend);
  13. inputText.text = "";
  14. }

And the code to listen to the ‘enter’ key(this is in both files):

  1. //Enter button to send
  2. var keyListener:Object = new Object();
  3. keyListener.onKeyDown = function() {
  4. if (Key.getCode() == "13") {
  5. sendButton.onRelease();
  6. }
  7. };
  8. Key.addListener(keyListener);

Download Source:

localConnectionRedBlue.zip

Tags: , , , , , ,

Related posts

flashden banner

Gravatar

4dplane said,

April 8, 2008 @ 10:19 pm

Hi, and thanks for this example. I have converted this into AS3 and put the code in .as files it works but in a strange way - here is a link to the problem if you are intersted.

http://www.kirupa.com/forum/showthread.php?p=2310148#post2310148

thanks again,
4dplane

Gravatar

Rajesh said,

May 16, 2008 @ 6:23 am

I have a doubt.. Can we create local connection between three swf files.. Can u please clarify my doubt…

Gravatar

Evan Mullins said,

May 16, 2008 @ 2:47 pm

Theoretically this is supported. Sorry, I haven’t tested it before, so I can’t say for sure of have an example, I’ll add it to my list though. =)

Gravatar

msden said,

June 24, 2008 @ 5:31 pm

Thanks so much! I’m glad that they’re helpful.

Gravatar

Taylor Dodds said,

June 25, 2008 @ 11:10 am

Is it possible to add this script to multiple buttons in a pamenu and call multiple flash vido files with it?

Gravatar

Taylor Dodds said,

June 25, 2008 @ 11:11 am

menu*

RSS feed for comments on this post · TrackBack URI

Leave a Comment