3
votes

I need some clarification on this subject as I just ran into an issue with loading swfs into a reused loader object.

So lets say I have 3 SWFs.
Main.swf
childA.swf
childB.swf

Main.swf has a loader object in it that gets reused (myloader.load("childA.swf")) and childA or childB swf will be loaded via user interaction.
Both child swfs have a com package with a class in that package called config.
The config files are different files for both classes just named the same.
both child swf also dispatch an event that the Main listens for

Now the problem I had was if childA was loaded first then after childB was loaded it would still show as childA. Basically, whichever one got loaded into that loader first would be the winner.
This drove me nuts as nothing I did would cause the swf to unload. Not until I found the following code.

var appDomain:ApplicationDomain = new ApplicationDomain();
var context:LoaderContext = new LoaderContext(false, appDomain);
_contentPanel.load(new URLRequest(str), context);

I stumbled over this code on a post somewhere talking about how to truly unload a swf. Apparently, This also applies to how to truly load a swf.
As you can see a new appDomain is created and assigned to the context when loaded.
This works like a dream I can now load and unload all day long.

My confusion is the event that the child dispatches still works, when I don't think the Main swf should pick it up due to it not being in the same appDomain.
I mean shouldn't the event be blocked?

1

1 Answers

2
votes

Unloading a SWF

The Loader class provides Loader.unload() (or after Flash Player vers. 10 - Loader.unloadAndStop())

Problem with the second loaded SWF being overridden by the first

Objects that are stored in ApplicationDomains are stored by their class-name and I wonder whether the class names of the loaded SWFs (or their children) are overriding. Even if that isn't the case; why not use a new instance of the Loader for each object being loaded?

How the Main SWF can pick up both children from another application domain

The Main SWF will be able to work with the new (loaded) application domains because they are child-domains of the Main SWF's (See ApplicationDomain.parentDomain). The Main SWF's domain will be the 'system domain' and the new instances will be loaded below it.

Splitting the loaded SWFs from the Loader

Ideally you want to have access to the SWF data irrelevant of the state of the Loader. You can do this by accessing the root movieClip of the SWF once loaded and create a new instance with

var rootClipClass:Class = ApplicationDomain.getDefinition("[InsertYourRootClipName]") as Class; 
var rootClip:MovieClip = new rootClipClass(); 

At that point you can unload the loader and work with your fresh instance cleanly.

Further reading