Have you ever come across a website containing banner ads of the same creative which appear to be in sync? Here I’m going to demonstrate how this is done by giving an example of how to use the LocalConnection class in Actionscript 3.0.
What is a local connection?
A local connection is used to communicate between two or more currently running SWF files. These can be either opened in Flash Player or, more commonly, embedded in a web page. A local connection object can be used to facilitate the communication between SWFs by enabling one SWF to call a function in another SWF.
A common place local connections are used is in banner advertising. An advertiser will buy out multiple ad positions on the same web page and create an advertisement that will utilize both spaces for greater impact and viewer retention.
A good use of this was the recent ‘Mac vs PC’ ads that did the rounds late last year. This ad featured our befuddled PC character attempting to light up a sign promoting Windows Vista featured in the above banner.
A local connection is important in this instance as both banners need to be in sync. Eveytime PC hit’s the button the lower SWF is calling a function in the upper SWF via a local connection object.
So how do you create a local connection?
Creating a local connection between SWFs is pretty easy to accomplish. Here I’m going to show a very simple local connection. This will feature 2 SWF files, where one SWF will call a function in the other.
First let’s create 2 new Flash documents called Receiver and Sender.
As the name implies Receiver will be our receiving SWF. This will contain a function which will be invoked by a user interaction in the Sender SWF. In my example below I have created a MovieClip on the timeline called ‘light’ which will play when triggered.
On frame 1 of our Receiver document, paste the following code.
import flash.net.LocalConnection;
var receiverLC:LocalConnection = new LocalConnection()
receiverLC.connect("_myConnection");
receiverLC.client = this;
function playMC():void {
light.play();
}
As you can see in the above code, we are first instantiating a new LocalConnection object and assigning it to the variable receiverLC.
We are then calling the connect method, passing in the connection string “_myConnection”. This string can be anything of your choosing however it is good practice to prefix this with an underscore (to find out why check out LocalConnection on Adobe LiveDocs).
The client property of our LocalConnection object needs to point to the object containing the function(s) we want to call. For the sake of simplicity we are using the ‘this’ keyword as our function exists in the same scope as our connection.
In this example the method we will be calling is playMC which in turn calls the play method the MovieClip object called ‘light’.
Next, on frame one of our Sender document, paste the following code.
import flash.net.LocalConnection;
import flash.events.MouseEvent;
var receiverLC:LocalConnection = new LocalConnection();
stage.addEventListener(MouseEvent.MOUSE_DOWN, sendData);
function sendData(e:MouseEvent):void {
receiverLC.send("_myConnection", "playMC");
};
Like our receiver, we need to instantiate a LocalConnection, this time assigned to the variable senderLC.
We then create an event listener which will call the sendData function when the user clicks anywhere inside this SWF. Inside this sendData function we call the send method of our LocalConnection object. To this we pass in 2 strings, our connection string and and the name of the function we wish to call. Note that the connection string, “_myConnection”, is the same as what we used in our Receiver file.
That’s it! If you publish both the Receiver and Sender documents and open them both with Flash Player you should have a working local connection. Try out my version below.
It is important to note that this is an example of using LocalConnection in it’s simplest form and is only communicating in one direction. For a more advanced example check out “Sending data with LocalConnection in Flash“.
To learn more about LocalConnection check out the Local Connection page on Adobe LiveDocs.

Pingback: Link Post Sunday 08/10 | Mr Sun Studios
some time ago i did http://nicholajs.borov.googlepages.com/loconPong.html for a friend as a demo of LoCon (he needed it for some banner thingy)
i think it could also be used for communicating stuff on webpage with desktop apps in AIR
Pingback: Tutorials | Flash/AS3 Tutorials Roundup « Flash Enabled Blog
Pingback: Sending data with LocalConnection in Flash | Ahrooga - Design | Development | Tech
About to start work on a “roadblock” banner of two ads talking to one another – thanks for the info, this will come in very handy!
Thanks. An excellent and simple explanation, and it was easily adaptable to my project.
Why this example doesnt work in IE7?