Tutorial details:
Written by: Jesse Stratford
Time: 25 minutes
Difficulty Level: Intermediate
Requirements: Flash MX
Topics Covered: How to use the new LocalConnection Objects in Flash MX to interact between SWF files.
Assumed knowledge: Variables, Paths, Objects, Methods, Flash MX Event Model.

Many of you will be familiar with the FSCommand Library of JavaScript methods, described in a tutorial on this site as well as elsewhere, which, until Flash MX was the best method for interacting between concurrently open SWF files. Many of you will also be aware of the many pitfalls of using FSCommands for this purpose, including incompatibility with certain browsers and operating systems. Local Connection Objects, like the FSCommand Library allow us to communicate between concurrently playing SWF files, but without the incompatibility issues, and with a lot less hassle.

When does such communication become useful? Consider the case of a website which uses HTML frames to separate the SWF navigation from the content loaded in another frame (or even a popup window). A common problem with such systems is that navigational decisions made by the user in the content frame are often not reflected in the navigation frame. For instance, if you visit the 'Profile' section of a site using the navigation, which highlights the 'Profile' button to indicate that's where you are, then you skip to the 'Pricing' section of the same site via a link in the content frame, how do you prompt the navigation SWF to deselect the 'Profile' button and highlight the 'Pricing' button to indicate the transition? Well, if you're lucky enough to own Flash MX, you use Local Connection Objects.

Here's a simple example. As you can see the table below contains two separate SWF files. When you type a string into the first and click 'Say!' it will send the same string to the second. This isn't exactly Earth-shattering stuff but it does illustrate the concept.

This SWF contains an input field with the variable name 'speech', and a button with the following code:

on (release) {
 talkingLC = new LocalConnection();
 talkingLC.send("simple_lc", "comeBack", speech);
}

The purpose of the code is explained below.

This SWF contains an dynamic field with the variable name 'tmp', and has the following code on frame 1:

listeningLC = new LocalConnection();
listeningLC.comeBack = function(speech) {
 tmp = speech;
};
listeningLC.connect("simple_lc");

See below for and explanation.

Let's examine the receiving SWF first. We begin by defining a new Local Connection Object called listeningLC. We can then attach to this object any methods (functions) we wish. In the example above I created a comeBack method which simply takes one argument and passes it into the text field on the stage - the name comes from the film Contact which I watched again last night. Finally we use the built in method connect to specify which other SWF we want to create a local connection with. Here, simple_lc is an arbitrary connection name I gave to the connection between my SWF files. We'll discuss connect in more detail below.

Continued overleaf...