Macromedia Flash MX Tutorial
Communications - Movie to Movie
Name: Movie to Movie
ID = Comm1 Version: 1 SWF file size = Posted: 2/03/03
Level: Intermediate/Advanced
Primary Focus: LocalConnection class
Secondary Focus: embedded movies
Approach: Data transfer from movie to another on the same HTML page.
Flash Movies
sender.swf
receiver.swf
ActionScript
sender.swf
//Communications
//Movie to movie on same HTML page
//Using LocalConnection()
//Author: Bruce L. Bird
// February 3, 2003
// SWF file name = sender.swf
// Initial Values
stop();
meterValue = 0;
inputLC = new LocalConnection(); // defines a local connection
// Slider
// slider is the instance name for the red slider movie clip
slider.onPress = function() {
this.startDrag(false,25,100,225,100);
}
slider.onRelease = function() {
this.stopDrag();
xvalue = slider._x;
meterValue = xvalue - 25;
xfactor = inputxf; // inputxf is the variable name for the X factor input text box
yfactor = inputyf; // inputyf is the variable name for the Y factor input text box
// send by local connection
inputLC.send("samePageLC", "receiverMethod", meterValue,xfactor,yfactor);
// In the line above "samePageLC" must be the same connection name as used in the receiver movie.
// "receiverMethod is the name of a function defined in the receiver movie.
// meterValue, xfactor, yfactor are variables whose values are sent to the receiver movie.
// Below is one method for checking whether the connection was made.
// messageBox is the variable name for a dynamic text box.
inputLC.onStatus = function(commFlag) {
if(commFlag.level == "status") {
messageBox = "Message Sent" ;
} else if(commFlag.level == "error") {
messageBox == "Connection Failed";
}
};
};
receiver.swf
// Communications
// Movie to movie on same HTML page
// Using LocalConnection class
// SWF file name = receiver.swf
// Initial Values
stop();
redDisk._xscale =10; // redDisk is the instance name of the red disk movie clip
// Local Connection
outputLC = new LocalConnection(); // defines a local connection
// Define receiver method
outputLC.receiverMethod = function(meterValue,xfactor,yfactor) {
output = meterValue; // from the slider position in sender.swf
xf = xfactor; // from the X factor input in sender.swf
yf = yfactor; // from the Y factor input in sender.swf
redDisk._x = 20 + meterValue;
redDisk._y = 110 + meterValue;
redDisk._xscale = 10 + Math.round(xf);
redDisk._yscale = 10 + Math.round(yf);
}
// Establish connection
outputLC.connect("samePageLC"); // The connection name, "samePageLC", must be
//the same in both sender and receiver movies.
// Close connection ??
//outputLC.close(); //If this line of code is made active,
// then communication fails. Perhaps the connection should be
// closed only after a check has been made that all the data
//has been received.Comments
The LocalConnection class is new to Flash MX. Descriptions and examples of this class are given in Moock (page 589) and in Tech Note #16243 at the Macromedia Flash Support Center. Additional discussion can also be found in Chun (page 226) and in the tutorial by Stratford.
While editing and testing this page with Microsoft FrontPage 2000 I found it necessary to close the page while in edit mode before the page would work in an Internet Explorer browser window. Also, as the ActionScript comments above indicate indicate, attempting to close the LocalConnection in the receiver movie with outputLC.close() (after I thought the data had been received) prevented the communication from working.
References
Chun, Russell, Macromedia Flash Advanced for Windows and Macintosh, Peachpit Press, 2003
Macromedia Flash Support Center
Moock, Colin, ActionScript for Flash MX - The Definitive Guide, 2nd Edition, O'Reilly, 2003
Stratford, Jesse, Local Connection Tutorial, www.actionscript.org
2002-7 All rights reserved. MINDdrops.com