The Receiver
Now we need to create the SWF that will receive the message we send from sender.swf. To do this, do the following:
Create a new Flash document, change its stage dimensions to 300 x 50, and save it as receiver.fla to your desktop.
Using the Text tool, create a dynamic text field on the stage, similar to the one shown in Figure 2. Remember to choose Dynamic from the Text Type menu in the Properties panel.
Figure 2 Create a dynamic text field and be the envy of all your friends.
With the text field still selected, enter receiveTxt into the Var field in the Properties panel. This assigns the variable receiveTxt to the text field. Also, enable the Show Border Around Text option.
Create a new layer named actions and enter the following code into the Actions panel to set an initial value to the receiveTxt variable:
var receiveTxt:String = "";
Next, we need to create a LocalConnection object for this movie, connect to the same connection that sender.swf connects to (myConn) and create the receiveMethod() that gets invoked by the send() method used in sender.swf. Follow these steps:
First, create the LocalConnection object, which needs to have the same name as the object in sender.swf:
var lc2:LocalConnection = new LocalConnection();
Next, create a connection to the myConn connection:
lc2.connect("myConn");
Finally, create the receiveTxt() method. To do this, we need to target the LocalConnection object and attach the method to it:
lc2.receiveMethod = function(txt){ receiveTxt = txt; }
Save your work, publish receiver.swf, and close receiver.fla.
As you can see, the receiveMethod() method contains a parameter named txt. This allows the method to receive one argument, which is sent by sender.swf. Remember, sender.swf needs to send a variable called txtVar to this function, so we must include a parameter in the receiveMethod() method to capture it.
When these two SWFs are embedded in a web page, the user will enter some text into the text field in sender.swf and click the Send button. When this occurs, the text is assigned to the txtVar variable; the LocalConnection object inside sender.swf creates a connection named myConn, calls the receiveMethod() method in receiver.swf, and then passes txtVar to it. The recieveMethod() method is invoked in receiver.swf and assigns the text that was passed to it to the receiveTxt variable so the dynamic text field on the stage can display it.
The last thing to do for this project is to create a web page to see it all work.