Sending data with LocalConnection in Flash

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.

This entry was posted in Adobe AIR, Adobe Flash, Advertising, Banners, Experiments, Tutorials, Uncategorized, Web and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

17 Responses to Sending data with LocalConnection in Flash

  1. xzone9 says:

    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.

  2. Dark Vyper says:

    This is brilliant! I honestly said “No way!” out loud. It reminded me of the DS a lot. Nice work!

  3. rahady says:

    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..

  4. Lance says:

    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. :)

  5. Daniel says:

    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?

  6. Will says:

    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.

  7. Sudheendra says:

    oh great! Its awesome example … waas searching for the one like this… thanks dude….

  8. stacey says:

    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()

  9. Arnold Aprieto says:

    Great Work! :)
    Is it possible to pass a variable from one swf to another with two browser pages using LocalConnection?

  10. Will says:

    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 :)

  11. Arnold Aprieto says:

    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.

  12. Sanjukta Samom says:

    Awesome Example…I was looking for a good example for passing data between swf files….This example answers most of my quires…Thanks dude!

  13. manfer says:

    This would be very nice method to pass data between swfs if it were working properly but it is not.

    Open two browser, or two windows of the same browser, or to tabs of same browser or whatever. Then, load there a page with this type of communication between two swf files. And look where the second loaded instance of the page sends the messages. BOTCH.

    It seems it is the way they decided to do it. The receiver string identifier must be something that is registered by the adobe player so other swfs can recognize it. But that makes it very very very easy to collide with another localconnections receivers of other swfs (probably many many people would set that string to “connection”, “myconnection”, “conn”…). Then lets look which one receives the messages and from where. Of course there is the domain limitation but even on same domain that can collide.

    You can just test with this page. Open it on two tabs of your browser and then click and throw the ball on the second tab. What happens? And as I mention it is not only on same browser if you load page on two different browsers it is the same. This is an example of collision on same domain.

    And other example of collision. If I use this on a banner that is shown on all pages of a site if a user opens in its browser two pages of that site (even not same page, just to pages of my site) the second page flash sender would send messages to the first page receiver. That makes no sense at all.

    I even tried to use it on a project where the send is almost at the begining of the swf and as soon as the message is received I close the connection and even destroy it (in this project was no more needed). But even that simple one that almost works, fails sometimes. It seems the message is not always sent, or it is delayed by other player tasks, I dont know exactly whats happening but fails. So even that simple one is not 100% working.

    I prefer sender to javascript, javascript to receiver that works as it is expected.

  14. Craig says:

    This example doesn’t seem to be working in FF 3.5.3 on the Mac.

  15. Robert says:

    Local connection does not work at all FF 3.5.3 Mac. At least not for me.

  16. Mark says:

    The example doesn’t seem to work with Safari 4 / Flash Player 10…

  17. Fred Tacon says:

    Will,

    Very cool example! I am using Local Connection for something far less exciting. I was curious, where can I find the code or instructions on writing the code for the physics of the bouncing ball? I would love to be able to recreate it. Not for commercial use, just for my own learning and enjoyment. If you would share your code, or if you could point me in the right direction, I would greatly appreciate it! Thanks!

    Fred

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">