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.

3 comments:

Orangej00s 14 December 2011 at 01:08  

thanks a lot =)

Online education 6 April 2012 at 05:17  

hi
I am not understand How to load / unload ".swf"

I have simple Code in AS3
Simple Proceed.
First swf Unload Then New swf load..

Plz Help me I am using Following Code

// First Swf On Next Button Script
stop();

movieClip_1.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF);

var fl_Loader:Loader;
var fl_ToLoad:Boolean = true;

function fl_ClickToLoadUnloadSWF(event:MouseEvent):void
{
if(fl_ToLoad)
{
fl_Loader = new Loader();
fl_Loader.load(new URLRequest("PowerVM_2.swf"));
addChild(fl_Loader);
}
else
{
fl_Loader.unload();
removeChild(fl_Loader);
fl_Loader = null;
}
// Toggle whether you want to load or unload the SWF
fl_ToLoad = !fl_ToLoad;
}

stop();

//ANd
// Second Swf On Prev. Button Script
ldr.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_5);

import fl.display.ProLoader;
var fl_ProLoader_5:ProLoader;

//This variable keeps track of whether you want to load or unload the SWF
var fl_ToLoad_5:Boolean = true;

function fl_ClickToLoadUnloadSWF_5(event:MouseEvent):void
{
if(fl_ToLoad_5)
{
fl_ProLoader_5 = new ProLoader();
fl_ProLoader_5.load(new URLRequest("Sybase ASE_PowerVM_1.swf"));
addChild(fl_ProLoader_5);
}
else
{
fl_ProLoader_5.unload();
removeChild(fl_ProLoader_5);
fl_ProLoader_5 = null;
}
// Toggle whether you want to load or unload the SWF
fl_ToLoad_5 = !fl_ToLoad_5;
}

ldr.addEventListener(MouseEvent.CLICK, fl_ClickToStopAllSounds);

function fl_ClickToStopAllSounds(event:MouseEvent):void
{

SoundMixer.stopAll();

}

My Swf is running Very Well But Overlap in Each Other.. I want Unload Previous SWF.
Plz Help ME.

Thnaks

Pramod mali

iliketo 8 April 2012 at 03:02  

@pramod,

Take a look at this post

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP