Sunday, May 9, 2010

Load and Control .swf files

In this post we will see how to load swf, how to control content in loaded swf and how to access it's variables.

To load swf simply we use loader and we will add event listener to load swf contents as:
Consider master.fla has following code:

var URLReq:URLRequest=new URLRequest("slave.swf")
var loader:Loader=new Loader();
loader.load(URLReq);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,LoadComplete);
var LoadedSWF;
function LoadComplete(e:Event):void
{
LoadedSWF=e.target.content;
addChild(LoadedSWF)
// You can access any Object in
LoadedSWF by using simply (.) dot operator.
//Example if LoadedSWF contain MovieClip named myMc, then you can access it as LoadedSWF.myMc
//You can  change properties of that MovieClip as LoadedSWF.myMc.x=100
}

var masterText:TextField = new TextField();
masterText.background = true;
masterText.autoSize = TextFieldAutoSize.LEFT;
addChild(masterText);

Send_btn.addEventListener(MouseEvent.CLICK,Send_btn_Clk)
function Send_btn_Clk(e:MouseEvent):void
{
LoadedSWF.SlaveText.text=masterText.text;
}



Here in,
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,LoadComplete);

EventListener is added to what loader loads i.e. what content loader is going to load, not to the loader.
So that we get how much bytes content loader loaded in progressEvent.

We assign loaded content to variable name LoadedSWF.
First trace(LoadedSWF) outputs undefined.
and second trace(LoadedSWF) it shows [object MovieClip] i.e. Loaded content is considered as MovieClip.


Consider slave.fla has following code:

var SlaveText:TextField = new TextField();
SlaveText.background = true;
SlaveText.autoSize = TextFieldAutoSize.LEFT;
addChild(SlaveText);



We can access SlaveText TextField to master.fla as LoadedSWF.SlaveText.

On send_btn click we directly get text in SlaveText into masterText as,

masterText.text=LoadedSWF.SlaveText.text;

Thank You,
Wish You Happy Flashing and Actionscripting..........

3 comments:

  1. good good keep it up

    ReplyDelete
  2. Hello Sachin,

    I just came across your code and I was wondering how you would do this for multiple loaded swf's movis clips

    Like an Intro and Outro :-

    swf1 - mc's tween on stage
    btn2 clicked - swf1 mc's on stage tween off stage | swf1 unloads | swf2 loads and tweens its mc's on stage

    then if

    btn3 clicked - swf2 mc's on stage tween off stage | swf2 unloads | swf3 loads and tweens its mc's on stage

    and so on....!!!

    ReplyDelete
    Replies
    1. Hi Mighty Flash Gordon,

      If you have animation in subSWF, it will animate in mainSWF once it is loaded by loader using loader.load(URLrequest).
      And you will get access of all contents by using "e.target.content".
      If you want to unload the loaded content then you can do,
      - loader.unload()
      - removeChild(e.target.content)
      - loadedSWF=null (if loadedSWF=e.target.content)

      once unloaded you can load another swf, to play its animation.
      To unload the same swf, do the same procedure as mentioned above.

      Delete