While working on a huge AS3 application last month, i discovered the magic of cross-scripting. What’s that s*** ? Following the livedocs :
If two SWF files written with ActionScript 3.0 are served from the same domain–for example, the URL for one SWF file is http://www.example.com/swfA.swf and the URL for the other is http://www.example.com/swfB.swf–then one SWF file can examine and modify variables, objects, properties, methods, and so on in the other, and vice versa. This is called cross-scripting.
But they don’t talk about the multiple problems you can find loading SWFs into SWFs. Here’s my first test with cross-scripting :

Let’s create two SWFs :
- main.swf (source) :
- loads two times part.swf
- has reference of the Singleton2 class (source)
- part.swf (source) :
- has a reference of the Singleton1 (source) and Singleton2 classes
Now, run the example and take a look at the output :
You can see that the Singleton1 is instanced two times… And even if the “Qualified Class Name” of the two instances are the sames, the classes are not equals (==) and each object is not of the type of the other one’s class…
But, the Singleton2 (which the main SWF has a reference) is only instanced once.
Conclusion : when a SWF loads a class that it doesn’t hold any reference, it creates an “alias” of this class.
var obj1:Object = part1.content["singleton1"];
var cls1:Class = part1.content["singleton1Class"];
// output : [object Singleton1]
trace(obj1);
// output : [class Singleton1]
trace(cls1);
// output : com.site::Singleton1
trace(getQualifiedClassName(obj1));
//////////////////////////////////////////////////
var obj2:Object = part2.content["singleton1"];
var cls2:Class = part2.content["singleton1Class"];
// output : [object Singleton1]
trace(obj2);
// output : [class Singleton1]
trace(cls2);
// output : com.site::Singleton1
trace(getQualifiedClassName(obj2));
//////////////////////////////////////////////////
// output : true
trace(obj1 is cls1);
// output : true
trace(obj2 is cls2);
// output : false
trace(cls1 == cls2);
// output : false
trace(obj1 is cls2);
// output : false
trace(obj2 is cls1);
trace("////////////////////////////////////////");
var obj3:Object = part1.content["singleton2"];
var cls3:Class = part1.content["singleton2Class"];
// output : [object Singleton2]
trace(obj3);
// output : [class Singleton2]
trace(cls3);
// output : com.site::Singleton2
trace(getQualifiedClassName(obj3));
//////////////////////////////////////////////////
var obj4:Object = part2.content["singleton2"];
var cls4:Class = part2.content["singleton2Class"];
// output : [object Singleton2]
trace(obj4);
// output : [class Singleton2]
trace(cls4);
// output : com.site::Singleton2
trace(getQualifiedClassName(obj4));
//////////////////////////////////////////////////
// output : true
trace(obj3 is cls3);
// output : true
trace(obj4 is cls4);
// output : true
trace(cls3 == cls4);
// output : true
trace(obj3 is cls4);
// output : true
trace(obj4 is cls3);
Check out the sources.

Hi,
Do you know how to implement this in Flex? I’m planning to build a framework using this concept..
The components and action script are in 2 different files in the server.
Mic
Hello,
I’m not sure of what you want to implement in Flex. But, the key of all this stuff above is in the LoaderContext class :
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/
When you load a file (here a swf) using the Loader class, you can specify a context : Loader().load(request,context);
The Context().applicationDomain property define whether or not the loaded classes will be part of the same application domain of your app (new references already loaded will be ignored) or a new application domain (new references already loaded will be duplicaded like the exemple in the post).