[an error occurred while processing this directive]

Community Publishing

Last Updated:
User Level:
Flash CS

Need more tips and tutorials?

SWF to SWF Communication when both swfs use actionscript 3.0

Q1:  How do you reference an object (movieclip, variable, function etc) in one swf from another swf?

A1:  It depends on the relationship between the two swfs (swf1 and swf2).

But, one general rule applies no matter the relationship:  once you know how to reference swf2's main timeline from swf1's main timeline, you can reference any object in swf2 from any object in swf1.  Specifically, you use dot-syntax to reference specific objects on (or nested in) the main timelines.  For example, if you want to call a function f2 on swf2's main timeline using actionscript from a movieclip timeline (eg, mc1 on swf1's main timeline), use:

MovieClip(this.parent).SWF1ToSWF2Connection.f2();  // this pseudo-code is on mc1's timeline

where SWF1ToSWF2Connection represents (in pseudo-code) the reference to swf2's main timeline from swf1's main timeline.  Creating that reference is the subject of the remainder of this discussion.

In all situations below it is assumed that you've imported the needed classes.


Q2: How do you reference the main timeline of swf2 from swf1 when swf2 is loaded into swf1?

A2:  After loading is complete, your loader's content property (cast as a movieclip or sprite) is a reference to the loaded swf's main timeline.


Here's sample code in your loading (swf1) swf:

// create a variable to reference swf2's main timeline
var swf2Main_mc:MovieClip;  // this assumes swf2 is a movieclip

// create a Loader instance
var loader:Loader = new Loader();

// add a listener for the complete event because you can't reference anything in the loaded swf until loading is complete.

// initiate loading of your external swf (swf2.swf):
loader.load(new URLRequest("swf2.swf"));

// create your listener function

function completeF(e:Event):void{
    // final step:  cast the loader's content property as a movieclip (or sprite) and assign your reference
    swf2Main_mc = MovieClip(loader.content);
    // do other things like add your loader to the display list, if not already done.  ie, use addChild().

You can now use swf2Main_mc to reference the loaded swf's main timeline any time after completeF() has executed.  And, you can then reference any object in swf2 that's defined in swf2's first frame.  

If you need to reference an object created on frame 33 of swf2, that frame will have to play before you can reference the object.  Named functions, like the completeF() above, are an exception.  They are availabe on frame 1 of their timeline even if they're not encoded until a later frame (including a frame that never executes) on the timeline.


Q3: How do you reference the main timeline of swf1 from swf2 when swf2 is loaded into swf1?

A3:  After swf2 is added to the display list (use the ADDED_TO_STAGE event), you can use this.root cast as a movieclip.


Here's sample code in your loaded (swf2) swf:

// create a variable to reference swf1's main timeline
var swf1Main_mc:MovieClip;  // this assumes that swf1 is a movieclip.

// create your listener to ensure this loaded swf has been added to the display list and can therefore obtain a 
// reference to its main timeline (which is always the main timeline of the loading swf).
this.addEventListener(Event.ADDED_TO_STAGE, initF);

function initF(e:Event):void{
    swf1Main_mc = MovieClip(this.root);


You can now use swf1Main_mc to reference the loading swf's main timeline any time after initF() has executed.  And, you can reference any object in swf1 that's defined when you execute your reference.


Q4: How do you communicate between two swfs (possibly in different domains) that are opened at the same time by the same user?

A4:  Use the LocalConnection class.


Here's sample code:

// this code goes in the receiving swf:    

// create a LocalConnection
var receive_lc:LocalConnection = new LocalConnection();

// allow communication from the sending swf

// start listening for a connection from the sending swf.  
// This must be executed before the sending swf applies the send() method to its LocalConnection instance.

// create a method/function for the sending swf to call.
function function_in_receiving_swf(received_arguments){
    // in the function, do whatever with the received_arguments sent by the sending swf.

// this code goes in the sending swf:     

// create a LocalConnection instance.
var send_lc:LocalConnection = new LocalConnection();

// apply the send method to your instance with two string arguments: 
// the connection name (always start with an underscore if the swfs are 
// in different domains and you can't hardcode the domain name) 
// and the method/function (as a string) you want to call in the receiving swf, 
// with a third argument that contains the string and/or numeric data you want to send to your receiving swf.


 There are more details and code examples in the flash help files.


Q5: How do you communicate between one swf opened at different times by the same user?

A5:  Use the SharedObject class.


Here's sample code in your swf:

// Create a SharedObject instance using the static getLocal() method.  
// The first parameter is the name of your SharedObject used by Flash 
// so you can have more than one SharedObject. 
// The second parameter is used by flash  to determine whether this 
// SharedObject is available to other swfs in your domain or is restricted 
// to particular  directories or subdirectories. 
// The forward slash is the most permissive path allowing your SharedObject 
// to be  available to all swfs in your domain. Check the SharedObject class
// in the Flash Help files for more information.

var so:SharedObject = SharedObject.getLocal("your_so_name","/");

// Check if variable1 has been assigned to the data property of your SharedObject and, if so, use it.

    greetings_tf.text =;

// Create a function used to assign variable1 to your SharedObject's data property.  
// Call this function when the data are ready to assign.  You are not limited to one variable.

function user_action_callsF():void{ = login_tf.text;
    // Apply the flush() method to your SharedObject to save the data immediately.  
    // Otherwise, under normal  circumstances, the data will be saved when the user exits your swf.




Q6:  How do you send data to a swf in a popup?

A6:  Append a query string to your html or swf path/file name. 

Here's sample code to send data (eg, variable1 and variable2):

// there no significant difference in sending data when calling a swf or an html.     
// variable1 and variable2 must be defined 
var s1:String = ""+variable1+"&var2="+variable2
navigateToURL(new URLRequest(s1),"_blank");

// or, with no significant difference in sending variables, call the swf directly
var s2:String = ""+variable1+"&var2="+variable2
navigateToURL(new URLRequest(s2),"_self");


Here's sample code to receive the data:

// There is a significant difference in the way the receiving swf parses 
// the data when the swf is called or its embedding html is called.   

// first, if you call the swf directly, code in the swf:
var var1_in_popup_swf = this.loaderInfo.parameters.var1
var var2_in_popup_swf = this.loaderInfo.parameters.var2;

// next, if you call the embedding html  in your swf's embedding html add a javascript 
// function that will retrieve the html's url (with the query string) 
// which will then be returned to your swf using the ExternalInterface class.

<script type="text/javascript"> 
function urlF(){
return location.href;

// in your swf, you can use the ExternalInterface class to retrieve the html's url from your javascript function:
var urlS:String ="urlF");

// finally, you can now use the flash string methods to parse the query string

function convertQueryStringToVariablesF(urlS:String):void{
    var queryS:String = urlS.split("?")[1];
    var varA:Array = queryS.split("&");
    for(var i:int=0;i<varA.length;i++){
        varA[i] = varA[i].split("=");
        this[varA[i][0]] = varA[i][1];
// this["var1"] and this["var2"] are now defined in the receiving swf 
// and have values equal to variable1 and variable 2 from the sending swf.


About the contributor

Keith Gladstien (kglad) is a prolific contributor to the Adobe Flash Forums who has been using Flash since version 4.  He has a background in mathematics (PhD, Purdue) and medicine (MD, Yale), and has created hundreds of Flash and AJAX applications for clients worldwide. 


Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License