[AS3] Loading External SWF into MovieClip using Loader Class in Flash ActionScript3

Tags:

Loading swf files in AS2 used to be a very simple thing, not so more in AS3.

In the old days of AS2, loadMovie(“external-file.swf”, containerClip) would do the job.

Enter AS3, now you first need to define a Loader class and then URLRequest path from where to load the external swf file location.

You also need an EventListener to your Loader object to be used for notifying that the swf has been loaded and is ready.

 
// This is the Loader instance that will load your SWF.
var swfLoader:Loader = new Loader();
 
// URLRequest points to your external SWF
var swfFile:URLRequest = new URLRequest("external-file.swf");
 
//create the container Movie Clip to load swf
var container:MovieClip= new MovieClip();
 
// Assign an event listener so that Flash informs you when the SWF has been loaded.
 
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadedHandler);
 
function swfLoadedHandler(e:Event):void {
	trace("swf loaded");
}
 
swfLoader.load(swfFile);
 
//just add the loaded swf to container
container.addChild(swfLoader);
//add container to the stage and make it visible
addChild(container);

Accessing Timeline of Externally Loaded SWF file from Main Movie

After your external swf movie has loaded, just store its reference in another movieClip after type casting it as MovieClip, and then you can access its time line easily for stopping, playing, pausing at any frame.

 
//store this loaded swf reference in currentSWF MovieClip object to access it later
//You need to cast swfLoader.content as a MovieClip before storing its reference
var currentSWF:MovieClip= new MovieClip();
 currentSWF = MovieClip(swfLoader.content);
 
//and you can easily access loader content's timeline like this
currentSWF.gotoAndPlay(10);
currentSWF.gotoAndStop(50);

Hope that helps.

Cheers!

Related posts:

  1. [AS3] Creating Preloader for External swf in Flash ActionScript3
  2. [AS3] Loading External swf & going to Specific Frame using Loader Class in ActionScript3
  3. [AS3] Load External swf into MovieClip & Play/Pause/Forward/Rewind and Stop it at LastFrame from Main Flash Movie
  4. Multiple External SWF Slideshow – Loading & Playing Mupltiple External SWFs in AS3 Flash Movie
  5. [AS3] Load External swf into Main Flash Movie with Play, Pause, Forward, Rewind, Load and Unload Buttons