Sending data with LocalConnection in Flash
Posted by Will | Filed under Adobe AIR, Adobe Flash, Advertising, Banners, Experiments, Tutorials, Uncategorized, Web
As a follow up to my previous post titled “How to communicate between multiple Flash files with LocalConnection” I’m going to demonstrate a slightly more advanced way of using the LocalConnection object in Flash.
Receiving SWF
Sending SWF
In the above example, click and throw the ball upwards.
Now you could be forgiven for thinking we are literally throwing the ball MovieClip from one SWF to the other, however this is merely an illusion. Both SWFs contain their own instance of the ball on the timeline, whose positions we are syncronizing with a LocalConnection object.
Sending SWF
In this example our sending SWF is passing along 2 parameters to a function called ‘positionBall’ which exists in the receiving SWF. These parameters are simply the x and y coordinates of the ball.
In our sending SWF we have an ENTER_FRAME event which handles the ball’s bounce physics as well as calls our LocalConnection’s send method. The send method invokes the ’setBallPosition’ function in our receiving SWF.
stage.addEventListener(Event.ENTER_FRAME,enterFrameListener);
private function enterFrameListener(e:Event):void {
/* Bounce code ommitted */
receiverLC.send("_myConnection", "setBallPosition", ball.x, ball.y);
}
Receiving SWF
On frame 1 of our receiving SWF we have the following code.
stage.addEventListener(Event.ENTER_FRAME, enterFrameListener);
var receiverLC:LocalConnection = new LocalConnection();
receiverLC.connect("_myConnection");
receiverLC.client = this;
var xPos:int;
var yPos:int;
function setBallPosition(xPos:int, yPos:int):void {
this.xPos = xPos;
this.yPos = yPos;
}
function enterFrameListener(e:Event):void {
ball.x = xPos;
ball.y = yPos+stage.stageHeight;
}
You’ll notice that we are adding the stage height on to the ball’s y position. Since the parameters we receive are relative to the sending SWF’s coordinates the y position needs to be offset so the bottom of the receiving SWF is equal to the top of the sending SWF. If we didn’t offset the coordinates with +stage.stageWidth our receiving SWF would appear to be an exact duplicate of the sender SWF.
In the example shown here we have ignored the space that exists between the 2 SWFs, though this could be easily added.
You may also be wondering why we aren’t just positioning the ball directly through the ’setBallPosition’ function. The reason for this is because even though we have 2 SWF’s running at the same frame rate their frame rates may not be in sync (or stay in sync). Handling the ball positioning inside the receiving SWF’s enterFrameListener makes sure the ball is only ever moved at the frame rate of the containing SWF and not at the frame rate of the sending SWF.
As you can see sending data between SWFs is a relatively trivial exercise and can be useful in creating some unique and engaging interactions.
For more information on the LocalConnection object be sure to check out Adobe LiveDocs.




September 4th, 2008 at 12:08 am
The above example is coded as a Class for Flash CS3. This is an excellent example of doing more than just sending a parameter to fill a textfield like most examples of a Local Connection.
The code example would be nice to have a complete working set to try out (the bounce code is not necessary).
With this example the user needs to configure the ball move and mouse actions.
Thank you.
September 9th, 2008 at 9:18 pm
This is brilliant! I honestly said “No way!” out loud. It reminded me of the DS a lot. Nice work!
October 15th, 2008 at 7:04 pm
Huahaha! Nice!
That’s cool. What makes it cool is the bouncing ball.
I’m not expected the demo will be like that
Because the last demo is just turning the light on/off..
February 17th, 2009 at 6:45 am
Very Cool. I have been looking for something along these lines. I’m trying animate text on the one I’m working on. Client has swf’s set up as two that simulate one swf and of course the designers didn’t know this. I’ll post a link when I get it working. I’m in As2 still.
May 9th, 2009 at 6:13 am
This is great, but I did notice that if you scroll the page so that you can’t see the receiving SWF, then throw the ball up then scroll back up the two are not in sync. Is this a problem with LocalConnection?
May 9th, 2009 at 6:26 am
Hmmm… You’re right. This *may* have something to do with my implementation of it. It would obviously be worth investigating it further if you plan on doing something with LC.
May 22nd, 2009 at 4:50 pm
oh great! Its awesome example … waas searching for the one like this… thanks dude….
May 23rd, 2009 at 7:42 am
This doesn’t work in IE7 with Flash Player 10? Do you know why or is it just me? It’s awesome in FF…
I get this error:
ArgumentError: Error #2082: Connect failed because the object is already connected.
at flash.net::LocalConnection/connect()
at Receiver_fla::MainTimeline/frame1()
May 26th, 2009 at 8:44 am
Great Work!
Is it possible to pass a variable from one swf to another with two browser pages using LocalConnection?
May 26th, 2009 at 8:50 am
Arnold,
I assume you mean two currently open browser pages. Like one open in Firefox, the other IE? I’m not 100% sure. I would guess that both SWF’s would need to be loaded from the same domain in order for it to work, if it works at all. This may be a security issue otherwise. By all means it’s worth trying! Good luck
June 2nd, 2009 at 8:57 am
Hi Will,
I tried creating a code but it didn’t work. It may be a security issue. I tried putting an event listener for each pages but to no avail. Hence, I just created a session variable. Primitive, but it gets the job done.
but I was hoping I could use the LocalConnection.
Thanks Again.