Sending data with LocalConnection in Flash
Published on Fri 15 Aug 2008 by Will
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
This content requires Flash.
Sending SWF
This content requires Flash.
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.
Did you enjoy this post?
Why not subscribe via RSS or follow Ahrooga on Twitter for alerts on new posts :)







Comments