Game Development Reference
In-Depth Information
improvement over the previous (and still available) unload method.
It makes sure that all listeners and sounds connected to the loaded
content are properly removed and garbage collected to prevent any
of the assets lingering in memory.
One thing to note about classes in separate SWFs is that, by
default, every SWF has its own
to store classes known as
its ApplicationDomain . This is to prevent classes in one SWF collid-
ing with those in another, which is helpful if two SWF files have
similarly named classes that are actually completely different in
their implementation. Most of the time, this is the behavior you
will want, as it protects your class integrity and keeps you from
thinking about how any other content may be built. However,
occasionally, you want to be able to merge a loaded SWF
sandbox
s Applica-
tionDomain with its container. A good example of this is a SWF
that contains nothing but sounds exported in the library. In order
to easily get access to the classes for these sounds, you would have
to go a roundabout way of looking them up. If you know that none
of the class names in your loaded SWF file will conflict with those
in the container, you can tell Flash to merge the two when the
SWF is loaded. Using the previous example, the loadResources
method would have to change.
'
protected function loadResources():void {
if (!resourceLoader) resourceLoader = new Loader();
var loaderContext:LoaderContext = new LoaderContext
(false, ApplicationDomain.currentDomain);
resourceLoader.load(new URLRequest( " resources.swf " ),
loaderContext);
resourceLoader.contentLoaderInfo.addEventListener
(Event.COMPLETE, resourcesComplete, false, 0, true);
}
The new code uses two classes from the system package:
LoaderContext and ApplicationDomain. When you perform a load,
you can specify the context under which the file is loaded. Inside
that context, you can determine which ApplicationDomain the
loaded file should use. By specifying the current domain, any class
definitions in the loaded SWF file will be combined with and
accessibletothoseinthecontainer.InChapter14,we
'
ll look at a
variation on this process when loading a set of assets.
One point to remember about using Loader objects is that you
must call unloadAndStop to fully unload any content you want to
get rid of. Simply setting the Loader object to null will only elimi-
nate the reference to it, and there is no guarantee that it will be
automatically garbage collected correctly. Fewer things are worse in
Flashthanamemoryleakthatcan
tbefixedbecausethereisno
attainable reference to the offending object.
'
Search Nedrilad ::




Custom Search