Problem:
You need to have a SWF file do something, based on something else that happened within a different SWF file.
Restrictions:
Must be Actionscript 2 (for example, because of banner ads, or to support older plug-ins)
Why?
Well, you might not want your site to be a giant Flash movie (thank you). You might have a small control, and want it to change a different interface or content element. You might have custom "rich-media" banner ads that need to interact with each other for a campaign (for some reason, I keep imagining a pong game, with a left paddle that's controlled by the user, a page of blog content, and a right paddle controlled by Flash...as if the blog is blocking the path of the ball. Probably too hard to play though... :-)
Regardless, the solution is using a local connection object!
Here's a quick example (download source files here):
The following spheres are two separate SWF files:
|
(this text is pure HTML -- view the source to prove it to yourself)
|
|
The left sphere is the "sender" and the right sphere is the "receiver". Click the left hand sphere to see how it sends a command to the right one (in this case it tells it to change color).
In the first movie (the sender or caller), we're going to have a sphere with a Play/Pause button. When you click it, it will change the button image, and send a command (via LocalConnection) to the listener (or receiver) movie.
var outgoing_lc:LocalConnection = new LocalConnection();
var flagPlay:Boolean = true;
theSphere.thePause._visible = false;
this.onMouseUp = function () {
if (flagPlay) {
flagPlay = false;
theSphere.thePause._visible = true;
theSphere.thePlay._visible = false;
outgoing_lc.send("lc_sphere_receiver", "methodToExecute", "play");
} else {
flagPlay = true;
theSphere.thePause._visible = false;
theSphere.thePlay._visible = true;
outgoing_lc.send("lc_sphere_receiver", "methodToExecute", "pause");
}
}
Pretty simple... all we really had to do was initialize the connection, and then send it a command (in this case "play" or "pause"). Make sure that the connection name (i.e. "lc_sphere_receiver") is the same in both movies!
Now, for the listener (aka receiver or destination) movie:
var incoming_lc:LocalConnection = new LocalConnection();
theSphere.theGreen._visible = false;
incoming_lc.connect("lc_sphere_receiver");
incoming_lc.methodToExecute =
function(param:String):Void {
if (param == "play") {
theSphere.theGreen._visible = true;
theSphere.theRed._visible = false;
} else {
theSphere.theRed._visible = true;
theSphere.theGreen._visible = false;
}
};
Basically, all we're doing is initializing the LocalConnection for this movie, and then creating a function that should be executed whenever it "hears" a command. We could create multiple methods as needed, or just use the one (as I have done) with passed parameters that tell it what to do.
Download the source files here!
6 Comments
| |
|
21
May 2009
|
Paulie Hi. Thank you, this is just what I want to do... however does not seem to work in Firefox for me. Works nicely in Safari though. |
|
21
May 2009
|
Paulie Hi. Thank you, this is just what I want to do... however does not seem to work in Firefox for me. Works nicely in Safari though. |
|
21
May 2009
|
Paulie Hi. Thank you, this is just what I want to do... however does not seem to work in Firefox for me. Works nicely in Safari though. |
|
21
May 2009
|
Jeffrey Did you have both pages open at the same time?
Interesting "bug" (related to LocalConnection):
If you load it first in Safari, and then load it in Firefox, but minimize the windows (so you can see both at the same time)...clicking either "Play" button will make one (and only one) of the balls change... |
|
29
Jun 2009
|
Pete I'm glad you posted that bug. I had the same problem during testing, but in reverse. Once I killed Firefox and refreshed the page in Safari, the Flash apps in Safari started communicating. |
|
26
Nov 2009
|
Jim Hi thanks for putting this up, I was wondering, if you can help me sync the two with each other once both are getBytesLoaded = getBytesTotal.
once they have both fully loaded, they play together.
I'm having a bit of difficulty with this.
please help, would be great |
Submit a Comment: