Overview:
Local Connection
Communication between two seperate flash files placed on the same page (or even running simultaneously on one machine) is a nice way to spread a project out. You can send variable, call functions, pretty much do anything in one swf from another. Easiest case use would be a navigation menu set up in one flash file to control the other one containing the content. I’ve made an example here showing how to send text from one to another. I’ve done it both directions here. Send text from the red swf to the blue swf, and from mr. Blue you send to the red flash file. I have named the flash functions in actionscript accordingly (or tried to, now I notice a few places I misspelled receive, ‘i’ before ‘e’, right? oh yea, except after ‘c’)…
Anyways, try out the example here, I made it a little easier by putting a keyListener on ‘Enter’, so you don’t have to actually press the send button. Didn’t realize it before, but this is like a chat app built in flash! So go ahead and chat with yourself to prove that it works!
Execute actionscript in one swf from another! Inter-swf communication.
Example:
Type here to send Red text to Blue flash file
And see it received here, and go ahead and send some back to Red.
Actionscript:
Red:
- // Receiving
- //create a local connection for reciept of text
- var receiving_lc:LocalConnection = new LocalConnection();
- //function called from other swf
- receiving_lc.recieveBlueText = function(textRecieved:String) {
- feedback.text += textRecieved+"\n";
- };
- //receive connection of specified name
- receiving_lc.connect("fromBlue");
- //Sending
- sendButton.onRelease = function() {
- //create local connection for sending text
- var sending_lc:LocalConnection = new LocalConnection();
- //put text from input into a var
- var textToSend = inputText.text;
- //send through specified connection, call specified method, send specified parameter
- sending_lc.send("fromRed", "recieveRedText", textToSend);
- //set the input empty
- inputText.text = "";
- }
Blue:
- // Receiving
- var receiving_lc:LocalConnection = new LocalConnection();
- receiving_lc.recieveRedText = function(textRecieved:String) {
- feedback.text += textRecieved+"\n";
- };
- receiving_lc.connect("fromRed");
- //Sending
- sendButton.onRelease = function() {
- var sending_lc:LocalConnection = new LocalConnection();
- var textToSend = inputText.text;
- sending_lc.send("fromBlue", "recieveBlueText", textToSend);
- inputText.text = "";
- }
And the code to listen to the ‘enter’ key(this is in both files):
- //Enter button to send
- var keyListener:Object = new Object();
- keyListener.onKeyDown = function() {
- if (Key.getCode() == "13") {
- sendButton.onRelease();
- }
- };
- Key.addListener(keyListener);


