I have two projects, A and B, that reference a library, MyExternalLibrary. A and B are .swfs (two different AS3 projects) and MyExternalLibrary is a .swc. When my application is run, A loads B.
There is a Class, MyLoader, in MyExternalLibrary that B uses extensively. MyExternalLibrary.MyLoader loads .swfs that are present on our server (we don’t embed them because they are loaded dynamically per user-request).
Note that our server contains A, B and all of the .swfs we are going to download.
Now, A must also load some of the .swfs on our server. Consequentially, A must also use MyLoader in MyExternalLibrary. So, In A, I add a reference to MyLoader, so that its binary is included during compilation. I only add a reference to MyLoader — I don’t actually instantiate or modify it. That is, somewhere in A‘s code (before B is loaded), I have:
MyLoader;
I (obviously) import the Class as well. This ensures that MyLoader‘s binary will be included in A‘s .swf.
Just referencing MyExternalLibary.MyLoader in A breaks B‘s functionality with MyLoader — all of the .swfs that B loads will not cast to its associated Class, despite being a MovieClip and having all of the proper fields. Furthermore, in the debugger, the loaded MovieClips show that they are of the correct Class. For example, if I load a .swf, it will be a MovieClip; moreover, the debugger states it is of type B.mySwfs.MyClass, but I cannot cast it to B.mySwfs.MyClass. I believe the culprit is ApplicationDomain.
Note that commenting out that line of code makes everything in B cast properly.
If anyone has any idea about what’s going on, I’d love some advice. I think that this issue deals with ApplicationDomain since A owns MyExternalLibrary.MyLoader, and I’m trying to cast a MovieClip loaded by A into a Class that is in B. When I comment out all references to MyExternalLibrary.MyLoader in A, B now owns MyExternalLibrary.MyLoader, so it is able to cast to classes in B. That is my hunch!
Thanks,
The solution was to ensure that
B‘sApplicationDomainis the same asA‘sApplicationDomain. This is done by parameterizing an instance ofLoaderContextin eitherLoader.loadorLoader.loadBytes.For example,
AloadsBwith the following line of code:m_loader.load(new URLRequest("B.swf"), new LoaderContext(false, ApplicationDomain.currentDomain));This adds all of
B‘sClassdefinitions toA. So, whenAloads a.swf, and thatMovieClipiscasted to aClassinB, everything will work becauseAandBare in the sameApplicationDomain. So, anObjectloaded byAcan becasted to aClassthat is inB.If anyone else encounters this problem, I highly suggest taking a look at GreenSock’s SWFLoader: http://www.greensock.com/as/docs/tween/com/greensock/loading/SWFLoader.html