ActionScript3:Using the Loader Class to load an external swf

From virtualMV2015wiki
Jump to: navigation, search
 Home  Flash11 (CS5) | Flash10 (CS4) <

Icon Objective.png


By the end of this page you will be able to:

  • Use the ActionScript3 Loader Class to load external assets into your ActionScript3 Movie at run time.


The Loader Class in ActionScript3 allows you to load external assets (including swf's) into your movie at runtime. This allows you to only load files as required and keeps the movie easy to manage and quick to load.

Setting the Main Stage

Create a new ActionScript3 Movie, in this example the stage is set at 400 x 300px.

  • Rename Layer 1 as background.
  • Insert a new layer and rename Buttons.
  • Insert and new layer and rename Actions.

Create a background on the background layer, and insert onto the stage in the buttons layer any number of buttons to correspond with the number of external swf files you intend to bring onto the stage. Give them each a unique name. In this sample, the buttons have been positioned over the background to give a tabbed appearance and are named homeButton, contactButton and worldButton.

Sd tabbedbgd.png

Save your movie.

The External swf's

Create two new AS3 movies at the size you require your imported movies to be. In this case they are set to the size of the yellow body portion of the main movie (387 x 242px).

Sd externalswf.png

Save the Fla's and compile the swf's.

The ActionScript

The ActionScript is inserted into Frame One of the Actions layer in the main movie. Make sure to name the swf's that the URLRequest is calling to that which you have named in the step above. In this example, each button has its eventListener removed when the corresponding swf is loaded (in effect disabling it). Two external movies are loaded, contactMovie.swf and worldMovie.swf. The homeButton unloads the loader and returns to the main movie.

var swfholder:Loader=new Loader();
worldbutton.addEventListener(MouseEvent.CLICK, loadWorld);
contactButton.addEventListener(MouseEvent.CLICK, loadContact);
function loadWorld(event:MouseEvent):void {	
	this.addChild(swfholder); // adds the swfholder loader
	swfholder.load(new URLRequest("worldMovie.swf")); // loads the required swf
	swfholder.x =6; //sets the x position of the loader
	swfholder.y =45;//sets the y position of the loader
	homeButton.addEventListener(MouseEvent.CLICK, unloadthis); // adds an eventlistener to the homebutton
	worldButton.removeEventListener(MouseEvent.CLICK, loadWorld); // disables the current swfs button
	contactButton.addEventListener(MouseEvent.CLICK, loadContact); // adds an eventlistener to the contactButton
function loadContact(event:MouseEvent):void {	
	swfholder.load(new URLRequest("contactMovie.swf"));
	swfholder.x =6; 
	swfholder.y =45;
	homeButton.addEventListener(MouseEvent.CLICK, unloadthis);
	worldButton.addEventListener(MouseEvent.CLICK, loadWorld);
	contactButton.removeEventListener(MouseEvent.CLICK, loadContact);
function unloadthis(event:Event):void {
	homeButton.removeEventListener(MouseEvent.CLICK, unloadthis);
	worldbutton.addEventListener(MouseEvent.CLICK, loadWorld);
	contactButton.addEventListener(MouseEvent.CLICK, loadContact);

Final Movie

A simple example of using the Loader Class to load external swfs into our main movie.

Try Me

Sd hottipicon.png HOT TIP! Create functions for repeated bits of code - save yourself some typing!

Related Information

For other ways to link up your AS3 swf's, check out these:

Icon References.png References


Day, S. (2010). ActionScript3:Using the Loader Class to load an external swf. Retrieved January 19, 2018, from