Page 1 of 2
Jesse was born and raised in Australia, and now lives in London. He is one of the original founders of http://ActionScript.org, and was formerly a Flash developer, teacher, author and speaker. While Jesse no longer works as a full-time Flash professional, he still enjoys actively participating in the http://ActionScript.org community as time permits.
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.
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:
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:
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...