- Home
- Tutorials
- Flash
- Intermediate
- Local Connection

Page 2 of 2
Jesse Stratford
Jesse was born and raised in Australia, and now lives in London. He is one of the original founders of http://ActionScript.org, and was formerly a Flash developer, teacher, author and speaker. While Jesse no longer works as a full-time Flash professional, he still enjoys actively participating in the http://ActionScript.org community as time permits.
The 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
7 Responses to "Local Connection" 
|
said this on 24 Mar 2007 10:19:05 AM CST
This article made it easy
As f http://www.kamalmeet.c Si I found one he http://www.adobe.c Th I Fred Gitelman |
|
said this on 23 Aug 2007 3:31:07 PM CST
Interesting article, and
|
|
said this on 02 Oct 2007 8:15:42 PM CST
nice, how about if in the
Many thanks, |
|
said this on 29 Nov 2007 4:42:14 PM CST
hey i got a movie to play
then in the i incomi if (pa _root.ball.play( } }; hope t |
|
said this on 22 Nov 2008 2:38:05 PM CST
i found this article help
|
|
said this on 24 Mar 2009 10:55:41 AM CST
Reminding me that I shoul
|
|
said this on 24 Mar 2009 3:15:44 PM CST
Reminding me that I shoul
The only think I might var receiver:Dyna try { receiver.connec } catch (e:Error) { t e } receiver trace("communicatio my_flvAut delete receiver.examp this.close(); // u }; function err // remov } |



Author/Admin)