Local Connection Actionscript – Communicate between seperate Flash files | Tutorial

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Receiving
//create a local connection for reciept of text
var receiving_lc:LocalConnection = new LocalConnection();
//function called from other swf
receiving_lc.recieveBlueText = function(textRecieved:String) {
feedback.text += textRecieved+"\n";
};
//receive connection of specified name
receiving_lc.connect("fromBlue");

//Sending
sendButton.onRelease = function() {
//create local connection for sending text
var sending_lc:LocalConnection = new LocalConnection();
//put text from input into a var
var textToSend = inputText.text;
//send through specified connection, call specified method, send specified parameter
sending_lc.send("fromRed", "recieveRedText", textToSend);
//set the input empty
inputText.text = "";
}

Blue:

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

//Sending
sendButton.onRelease = function() {
var sending_lc:LocalConnection = new LocalConnection();
var textToSend = inputText.text;
sending_lc.send("fromBlue", "recieveBlueText", textToSend);
inputText.text = "";
}

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

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

Download Source:

localConnectionRedBlue.zip

  • del.icio.us
  • Digg
  • E-mail this story to a friend!
  • Facebook
  • FriendFeed
  • Google Bookmarks
  • LinkedIn
  • Mixx
  • Print this article!
  • Turn this article into a PDF!
  • StumbleUpon
  • Technorati
  • Twitter
  • RSS

Related posts

Visit Circlecube @ FlashDen
Stomping the Search Engines 2, from StomperNet

  • 4dplane
    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=23...

    thanks again,
    4dplane
  • Rajesh
    I have a doubt.. Can we create local connection between three swf files.. Can u please clarify my doubt...
  • circlecube
    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. =)
  • Thanks so much! I’m glad that they’re helpful.
  • Taylor Dodds
    Is it possible to add this script to multiple buttons in a pamenu and call multiple flash vido files with it?
  • Taylor Dodds
    menu*
  • julian
    good job,, thanks for this tutorial...
  • motu
    I too am interested to control a Flash video player from a separate Flash file (with buttons or menus sending the controls)...

    BTW, here's an example with 4 Flash files communicating on one html page:
    http://home5.inet.tele.dk/nyboe/flash/localconn...

    Thanks!!
    motu
  • ruchi
    its a good example ,easy to learn,understand..........
  • Well on Firefox for Mac your exemple does not work, weird....
  • circlecube
    With ff3 on my mac it works for me ;)
  • ameer
    hey can any body help
    it is not working in ie6...plz help
  • This example works great but what can be done about someone who has two browser events open at the same time. The connection will only address the first page that is opened.
blog comments powered by Disqus