Sending data with LocalConnection in Flash
Posted by Will | Filed under Adobe Flash, Advertising, Banners, Experiments, Tutorials
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..