Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Design > Adobe Creative Suite

  • Print
  • + Share This
From the author of

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:

  1. Create a new Flash document, change its stage dimensions to 300 x 50, and save it as receiver.fla to your desktop.

  2. 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 2Figure 2 Create a dynamic text field and be the envy of all your friends.

  3. 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.

  4. 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:

  1. First, create the LocalConnection object, which needs to have the same name as the object in sender.swf:

    var lc2:LocalConnection = new LocalConnection();
  2. Next, create a connection to the myConn connection:

    lc2.connect("myConn");
  3. 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;
    }
  4. 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.

  • + Share This
  • 🔖 Save To Your Account