LocalConnection: Hot Flash-on-Flash Communication
LocalConnection: Hot Flash-on-Flash Communication
Written by craig coffman
How many of you Flash developers out there have had two separate movies in an HTML file and wished they could talk to each other? I know I have. On several occasions. I know that you can go the JavaScript route and use fscommands. But, frankly, to me they are hassles to put in place. If for no other reason because you have to use an entirely different scripting language. Wouldn't it be nice if this could all be done in lovely actionscript? Well, in some cases at least, it can.
The solution is a LocalConnection Object. Perhaps you have heard of this, or perhaps not. I had not. Now that I have, I wish I had known it for years. This handy-dandy Object will add interactivity between two separate movies in one page, or even in different windows (this is reported and not verified by myself). The best part, it is really easy to do.
Basically, you create two movies, one to send and the other to receive. Technically, they could both send and receive, but for this we will only have it go one way. Let's look at the sending movie.
Here is the code with comments:
// GIVE THE BUTTON A FUNCTION
buttonInstance.onRelease = function() {
// CREATE THE LOCALCONNECTION
outgoing_lc = new LocalConnection();
// SEND USING 'SEND' METHOD
outgoing_lc.send("lc_name", "methodToExecute", "click");
// NOW DELETE LOCALCONNECTION
delete outgoing_lc;
};
That is it. Very easy. You simply put a button (in this case) on the stage. Assign it the function and save the .swf. The code creates and opens the LocalConnection, passes a variable 'lc_name' which the receiving movie looks for, then deletes the LocalConnection.
Now to the receiving movie code with Comments:
// CREATE THE LOCALCONNECTION
incoming_lc = new LocalConnection();
// CREATE FUNCTION TO CALL AFTER CONNECTION IS MADE
incoming_lc.methodToExecute = function(param) {
// TELL THE MOVIE WHAT TO DO
ns.pause(true);
};
// MAKE THE CONNECTION
incoming_lc.connect("lc_name");
Another simple code block. In this case, we create a LocalConnection and define a function to execute. This example will pause a playing video. Finally, we give the newly created LocalConnection the 'channel' to monitor for information called 'lc_name'. When something comes through with that name, it triggers the function to pause the movie.
That is it. Extremely simple. This can be used to pass any sort of information you would like. As long as you can write the ActionScript, this will let one movie tell another independent movie what to do. You can download the source by clicking this link.
I say in some cases because I have only tested this in the most recent project I am building, which uses the Flash 6 player. With many newer players out there, I am not sure if this method will work or not. For me, though, Flash 6 is my most requested target so it works like a charm.
If you have used this on newer versions to success, please let me know.