Monday, 22 November 2010

Load external swf using SWF loader

I wrote a tutorial about how to load external swf’s using the native Loader class about a year ago. Within this time a new AS3 loading system by greensock.com, the person who created TweenLite, has been released. Its called LoaderMax and it simplifies and enhances the process of loading external assets. Apparently, it’s more stable and reliable for loading external asset than the Adobe loading classes. In this post I will show you how to load an external swf’s using the SWF Loader class from LoaderMax.

To simply load an external swf, you can use the following code below:

//Import the packages needed.
import com.greensock.*;
import com.greensock.loading.*;

//Create an instance of the SWFLoader class. The container property is where
//the loaded swf will be added. The ‘this’ value means it will get added to the stage.
//If you want to add the external swf to a movie clip you can change this value.
var loader:SWFLoader = new SWFLoader("example.swf", {container:this});

//Load Swf
loader.load();

However, SWF Loader has 27 additional properties that you can use. Below is an example some of them. There are also more powerful properties such as: scaleMode, hAlign, vAlign, crop and centre registration point. Checkout the SWF Loader documentation for all the properties.
import com.greensock.*;
import com.greensock.loading.*;

var loader:SWFLoader = new SWFLoader("example.swf", {container:this, x:100, y:100, alpha:0.5, scaleX:0.5, scaleX:0.5, rotation:50});

loader.load();

The event handlers in SWF loader are added in the same way. The example below fades in the external swf after it has been loaded.
import com.greensock.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;

var loader:SWFLoader = new SWFLoader("example.swf", {container:this, alpha:0, onComplete:completeHandler});

//Load Swf
loader.load();

function completeHandler(e:LoaderEvent):void{
    TweenLite.to(e.target.content, 1, {alpha:1});
}

You can access the root of the external swf using the ‘rawContent’ property. This gives you access to the functions and the main timeline of the external swf. The example below calls a function called testing() from the external swf.
import com.greensock.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;

var loader:SWFLoader = new SWFLoader("example.swf", {container:this, alpha:0, onComplete:completeHandler});

//adds the loader content to the display list
//before raw content is loaded.
addChild(loader.content);  

//Load Swf
loader.load();

function completeHandler(e:LoaderEvent):void{
    TweenLite.to(e.target.content, 1, {alpha:1});

     var mcTemp:MovieClip = loader.rawContent; 

     mcTemp.testing();
}

If you want to access an external swf function using the native AS3 loading classes, take a look at this tutorial.

  COPYRIGHT © 2011 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP