We are going to create a simple example that will show you the basics to connect two SWF together. We are going to create a movie that will send a message to another movie when the "send message" button is pressed.
The concept is pretty simple, we have a movie that send a connection with a particular ID and we have a movie that is listening for a connection with that particular ID. If the listening movie detect a connection then it connects to it and the method called by the sending movie will execute in the listening movie. Let's have a look at the final version below :
Let's have a look at the code in the sending Movie first :
sending = new LocalConnection();
myButton.onPress = function(){
sending.send("myConnection","displayMessage",myMessage.text);
} |
Nothing very complicated here, we create an instance of the localConnection class by declaring :
| sending = new LocalConnection(); |
Then we create the code will trigger the connection when the "send message" button is pressed. We use the method send to initiate a connection. The send method has 2 parameters : the connection ID or connection name ( myConnection in our example) and the method to call in the listening movie once the movies are connected ( displayMessage in our case). There is an optional 3rd parameter that we are using in our example, that last optional parameter is used to pass arguments to the method sitting in the listening movie. In our case we send the message typed in the input field to the listening movie.
Let's now have a look to the code which is in the listening movie :
receiving = new LocalConnection();
receiving.displayMessage = function(receivedMessage){
myMessage.text = receivedMessage;
}
receiving.connect("myConnection"); |
Again here we need to create an instance of the localConnection class
using the
new constructor.
The we defined the method that is going to be called when the connection is established. In our case the method will be displaying the message passed as an argument from the sending movie.
Finally we use the connect method to start listening for an incoming connection. It is very important that both the send and connect method use the same connection ID, myConnection in our case.
For more information on the localConnection class I suggest you have a look at you Flash manual or go to the Adobe Livedocs.
Good luck :)
|