- Home
- Tutorials
- Flash
- Intermediate
- Local Connection

Page 2 of 2
Jesse Stratford
Jesse lives and works in Melbourne Australia. He is the Cofounder of http://ActionScript.org. A Flash enthusiast, teacher, author, freelancer and speaker Jesse's main focus nowadays is managing http://ActionScript.org, but he enjoys participating actively in community and the wider Flash scene when he has time.
View all articles by Jesse StratfordThe first line of the sending code simply establishes a new Local Connection Object as before. Note that the name is not the same (and does not have to be the same) as the Local Connection Object in the receiving SWF. The connection name is what tells Flash which SWFs are to communicate with each other; notice it is the same in the connect method for the receiver as in the send method (below) for the sender; this is required for the Local Connection to execute successfully. The second line is what does all the work. The built in send method accepts multiple arguments; the first is the name of the connection between the SWF files, which in this case is simple_lc as defined above; the second is the name of the method you wish to invoke in the receiving SWF, in this case comeback; the remaining arguments are passed as arguments to the method you name. For instance, in the above example, the variable speech is passed to the comeback method in the receiving SWF, which results in the speech variable being displayed in the tmp text field.
Sometimes a connection will fail, due to improper coding or perhaps a domain conflict (see below). Defensive programming teaches us to check for such cases so we don't continue as though everything is OK when it is in fact not, and Macromedia provide us with the onStatus handler which is perfect for performing such checks. The onStatus handler for Local Connections is invoked upon the sending Local Connection after a send command. It returns an object with a level property which can help us determine if our send command was successful. If the level property is equal to the string "error" then something has gone wrong. Change the code in your talking SWF to this:
on (release) {
talkingLC = new LocalConnection();
talkingLC.send("simple", "comeBack", speech);
// Note in the line above the connection name is wrong
// "simple" should be "simple_lc" as before. This is intentional.
talkingLC.onStatus = function(result) {
if (result.level == "error") {
speech = "Connection failed!";
}
};
}
When you execute this code it will inform you that your connection failed, in this case it's because your connection name is not the same in your sending and receiving SWFs.
There are two other aspects of Local Connections which you should know about. The first is the close method and you get no points for guessing what it does. Closing a Local Connection when it's no longer needed is good form. Can you figure out what would happen if I changed my receiver code to that below?
listeningLC = new LocalConnection();
listeningLC.comeBack = function(speech) {
tmp = speech;
this.close();
};
listeningLC.connect("simple_lc");
The answer is that, while in the working example above you can alter the text and click Say again in order to update the display in the receiver, if my code included a close reference the first string sent would be permanent and unchangeable. Deleting a receiving Local Connection object using delete has the same effect.
Finally, consideration needs to be given to cases where both files do not reside in the same domain. Macromedia have gone to great lengths to ensure the Flash MX player is secure while at the same time allowing us to interact selectively between files across domains, especially in the case of Local Connections. If you find that your Local Connections are not performing (and even if they are) you should read the MX Security Whitepaper (PDF), specifically the section on domains and interaction between them. It will take you five minutes and save you lots of headaches in the future. If you've established that you are indeed suffering from a domain conflict, you can resolve the issue using the allowDomain and domain Local Connection methods. These are described extensively in Macromedia's Local Connection documentation.
So there's a 25 minute crash course in Local Connections for you! Thanks to Macromedia for their good docs on this. If this tutorial helped you out please drop me an email and let me know. I'm open to corrections and suggestions as always.
| Jesse Stratford is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash. NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there. |
If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity. |
| This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it. |
Spread The Word
Related Articles
5 Responses to "Local Connection" 
|
said this on 24 Mar 2007 10:19:05 AM CST
This article made it easy for me to get started with LocalConnection (thanks!), but I soon ran into problems.
As far as I can tell a LocalConnection can "break" if you give it enough time and especially if you do multiple sends one after the other. When I first saw this happen I assumed it was a problem in my code, but I checked carefully and found no obvious bugs. Similar problems are reported here: http://www.kamalmeet.com/wordpress/2005/06/23/networking-in-flash/ Since the workaround suggested on this site sounded like a nightmare to code, I looked for another way to communicate between my SWFs. I found one here: http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=60&catid=585&threadid=1191096&highlight_key=y&keyword1=swfloader This technique will work only if you use SWFLoader from your main SWF to create your other SWFs. I am still a new ActionScript programmer. Sorry in advance if some of this is wrong, but I thought my finding might help other programmers avoid the lengthy (and frustrating) experience of trying to figure this out. Thanks to those involved in creating and maintaining this great site :) Fred Gitelman |
|
said this on 23 Aug 2007 3:31:07 PM CST
Interesting article, and really handly tool for talking between two swf's. Just wish there was a way to accommodate for multiple instances of the same flash files being open at the same time, such as a user browsing your site in two windows at once...
|
|
said this on 02 Oct 2007 8:15:42 PM CST
nice, how about if in the "outcoming.swf" contains buttons and in the "incoming.swf" contains each movies. My question is how to get button "a" to play movie "a" in the incoming.swf?
Many thanks, |
|
said this on 29 Nov 2007 4:42:14 PM CST
hey i got a movie to play in the incoming swf by telling the outgoing swf to send a variable.. say '1'
then in the incoming swf i replaced the script with: incoming_lc.methodToExecute = function(param:String):Void { if (param = "1") { _root.ball.play(1); } }; hope this helps |
|
said this on 22 Nov 2008 2:38:05 PM CST
i found this article helpful to get me started. But I ran into a problem on Macs if I use more than 7 instance of a button that uses a local connection to communicate with a control swf that keeps track of the buttons' states and updates them. I found that this problem does not happen on Windows machines. Is this a known problem with local connections and Macs?
|




Author/Admin)