Game Development Reference
In-Depth Information
Working with Multiple SWF Files
At some point, you
ll probably be in the position of using multiple
SWF files to support a game. Perhaps you have multiple game
levels, each in their own SWF, or you have externalized all your
audioaseparatefile.ToloadexternalSWFfilesatruntime,you
'
ll
need to use a Loader object, which is part of the display package.
The syntax looks like the following:
'
package {
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLRequest;
public class LoaderExample extends Sprite {
protected var resourceLoader:Loader;
protected var resources:MovieClip;
public function LoaderExample() {
loadResources();
}
protected function loadResources():void {
if (!resourceLoader) resourceLoader = new
Loader();
resourceLoader.load(new URLRequest
("resources.swf"));
resourceLoader.contentLoaderInfo.addEventListener
( Event.COMPLETE, resourcesComplete, false, 0, true);
}
protected function resourcesComplete(e:Event):void {
resources = e.target.content as MovieClip;
}
protected function unloadResources():void {
resourceLoader.unloadAndStop();
}
}
}
In the loadResources method of this example, a new Loader
object is created (if one doesn
'
t already exist) and is used to load a
SWF named
resources.swf.
A listener is then added to the
Loader
s contentLoaderInfo object, which will dispatch events about
the Loader
'
s progress. Once the load has completed, the resources
variable is assigned to the content of the Loader. If at some point
the data needs to be unloaded, the method unloadResources can
be called to dump the SWF. Developers familiar with AS3 already
will note that the new unloadAndStop, introduced in CS4, is a big
'
Search Nedrilad ::




Custom Search