Local Connection Actionscript – Communicate between seperate Flash files | Tutorial

Overview:

Local Connection
Communication between two separate 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

Get Adobe Flash player

And see it received here, and go ahead and send some back to Red.

Get Adobe 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

This entry was posted in tutorial and tagged , , , , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.
  • 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=2310148#post2310148

    thanks again,
    4dplane

    • http://circlecube.com Evan Mullins

      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. =)

  • Rajesh

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

  • http://www.psoral.net msden

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

    • Taylor Dodds

      menu*

  • Taylor Dodds

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

  • julian

    good job,, thanks for this tutorial…

  • ruchi

    its a good example ,easy to learn,understand……….

  • http://www.opssm.com laurent xénard

    Well on Firefox for Mac your exemple does not work, weird….

  • http://circlecube.com Evan Mullins

    With ff3 on my mac it works for me 😉

  • ameer

    hey can any body help
    it is not working in ie6…plz help

  • http://xzone9.com xzone9

    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.

  • Vance

    you may want to check the code and solve for the problem that when someone opens your website up in two separate tabs, the local connection object fails. you did catch the error correctly however you didn’t give a second or third one to try when they fail. i’ve had this problem before and it saved me to have a second connection name to try if the first one failed. also if the user was at a website that used the same code you have, more specifically the same local connection name you used, one of the connections would fail.

    just something to think about when doing this. i’ve found the local connection is a real pain.

  • Tim

    This code isn’t working for me on OS 10.5, Firefox 3.5.6, Safari 4.0.4, Flash Player 10,0,32,18. Not sure why.

    Works fine via Parrallels / Windowx XP Pro 2002, IE 6.

    Is this Flash Player Mac bug?

    • http://circlecube.com Evan Mullins

      Well this is almost 2 years old, so I wouldn’t be too surprised it new browser updates may have borked it. i’d try some as3 by now to do it. That may help as well

      • WE

        That’s the conclusion I’m starting to come to. I’ve been searching around for a working version of a flash localConnection tutorial for hours. I have yet to find a functioning example. Unfortunately, I’m also unable to find any verification that this is due to some new security feature, or other known issue (aside from the very common multiple open browser issue). Seems that this is a problem that has slipped through the cracks and isn’t being addressed anymore. Is there some new way to communicate between flash files on an HTML page?

  • Matt D.

    Hi,
    I’m just starting to learn ActionScript & 3.0 and am working on a project that uses a set of 4 navigation buttons (swf1) that on user click need to play their respective content menu swf’s (swf 1-4) in the same html page. (basically the case that you describe in your overview above).
    Can you provide an example of how to code one of the 4 movie clips in swf1 to call it’s swf content menu and play it from frame 2?
    Any help would be greatly appreciated.
    Thanks in advance.
    Matt

  • http://fallingcheetah.com Nite

    hi do u have example with 3 swf and localconnection
    thanks

  • Pingback: Flash LocalConnection Tutorials | tutorials blogs()

  • Lou Parker

    The example does not work in FF6 or IE9. Any idea why that is?

  • http://www.servelec.pt Miguel Barata

    Is it possible to have the red flash in one computer and the blue flash in another computer communicating across the web?

    Great example
    Regards

    • http://circlecube.com Evan Mullins

      @Miguel

      This is indeed possible, but this is not the method that will do that. You’d need to communicate with the server for something like that. Best of luck!

      • http://www.servelec.pt Miguel Barata

        The idea is to control a remote swf by pressing buttons on a local swf. Let’s say I have a computer with a swf loaded on a browser and from across the web I press a button on another swf also loaded on a browser, an that pressing of the button makes an action on the first swf.

        • http://circlecube.com Evan Mullins

          Yes Miguel, this case would require some communication with the server. So each swf would talk to either a database or an xml or even a text file.you’d have one (our even both) writing data while the other reads it. Maybe look up chat apps in flash, I remember seeing some like that not to long ago (maybe on active Tuts . net), it would show you how to use a technique like this.

          • http://www.servelec.pt Miguel Barata

            Thanks Evan, I’ll look up for that kind of app.

            BTW Great Avatar! 😀

  • Rob

    Instead of sending text, can I send code ex: gotoandplay(20). This code will come from red and sent to blue and blue will start in frame(20)

  • Recent Posts

    Adactio: Journal—Instantiation

    There needs to be a cultural change in how we approach building for the web. Yes, some of the tools we choose are part of the problem, but the bigger problem is that performance still isn’t being recognised as the most important factor in how people feel about websites (and by extension, the web). This […]

    human_time_diff « WordPress Codex

    Determines the difference between two timestamps.The difference is returned in a human readable format such as “1 hour”, “5 mins”, “2 days”. Source: Function Reference/human time diff « WordPress Codex Used this little known, but nice gem today. It’s a core WordPress function that gives human relative time from any other datetime in “U” format. […]

    logo

    Cyclomatic Complexity: Logic in CSS – CSS Wizardry – CSS, OOCSS, front-end architecture, performance and more, by Harry Roberts

    I recently hit upon a way of thinking that made me realise that CSS does include logic, and the fact that it’s rarely viewed as such is probably also why we end up with such poor CSS at all.I found myself explaining compound selectors to a client as being made up of the subject—the thing […]

    Screenshot-2013-11-27-14.20.16

    Add Links to Twitter Mentions, Hashtags, and URLs with PHP

    This gem helped me out today as I was working with the Twitter API to create a custom WordPress widget. I was about to do exactly this and was not looking forward to it, luckily I did a quick search and found this webtips post. Thanks! If you’re using the Twitter v1.1 API to fetch […]

    regex101.com

    Online regex tester and debugger: JavaScript, Python, PHP, and PCRE

    Stumbled on a great regex tool today. An explanation of your regex will be automatically generated as you type. Detailed match information will be displayed here automatically. via Online regex tester and debugger: JavaScript, Python, PHP, and PCRE. Tweet

    CSS Dig

    A chrome extension for analyzing your CSS. Check your properties (and reuse – are they following DRY principles?) and your CSS selectors (and their specificity). Analyze your CSS in a new way. Consolidate, refactor, and gawk at the 37 shades of blue your site somehow ended up with. Take a look at all your CSS […]

css.php