Thursday, September 28, 2006

SWFLoader : Dynamically load / unload my 'sub applications' in Flex 2.0

Dynamically load / unload my 'sub applications' in Flex 2.0 (compiled swf) but user feel should be as if only one mxml has been loaded.

One problem in flex...

You have 3 .mxmls that get compiled into .swf ( Say A , B , C . )

step1 You have to load B inside A
step2 In B there is a button on click you have to unload B from A and load C
step3 Also in C there is a button on click you have to unload C from A and load B

---

My approach

for Step1 : cool!
I am loading B in A using SWFLoader.

for step2 : stuck!
I am not able get hold of the instance A from which i can change source property of swfLoader (instance) to "C.swf"

for step : whatever works for step 2 :-(


--
Solution ;)

Two days after making this post I figured out a simple way to achieve this.
Code below demonstrates the my approach.

Thanks,
Vishwajit Girdhari
Flexblog : http://flexiness.blogspot.com

Main.mxml

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"

creationComplete="init(event)" >


 <mx:Script>

  <![CDATA[   

   import mx.controls.Alert;

         import flash.events.Event;

         private function init(e:Event):void
{

          //doing nothing

           }

   public function loadSWF (filename : String) : void
{       

          swfLoader.source=filename;

         }    


       ]]>

 </mx:Script> 


   <mx:SWFLoader id="swfLoader"  source="one.swf"  
></mx:SWFLoader>

</mx:Application>

 

one.mxml

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">

<mx:Script>

 <![CDATA[

  import mx.controls.Alert;

  public function handleOne ( event : Event ) :void {

      mx.core.Application.application.loadSWF("two.swf");     
 

  }

   

 ]]>

</mx:Script>

<mx:Button id="btnOne" label="Button One" click="{handleOne(event)}"
y="200"></mx:Button> 

</mx:Application>

 

two.mxml

<?xml
version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml"
layout="absolute">

<mx:Script>

 <![CDATA[

  import flash.profiler.showRedrawRegions;

  import mx.controls.Alert;

  

  public function handleTwo ( event : Event ) :void {

   mx.core.Application.application.loadSWF("one.swf");     
 

  }

 ]]>

</mx:Script><mx:Button id="btnTwo" label="Button Two"
click="handleTwo(event)" x="300"></mx:Button> 

</mx:Application>



2 comments:

Anonymous said...

Hi, a while ago I asked some questions on your blog and it was really helpful. Recently I'm completely stuck on something again.. was wondering if you could help =)

It seems like a simple problem. I'm trying to load different youtube videos at runtime as external swfs. But only the first one loads, others just don't get displayed.

So I have a swfloader and two buttons to switch source.


public function loadSWF (filename : String) : void {
swfLoader.source=filename;
}


< mx:SWFLoader id="swfLoader" source="http://www.youtube.com/swf/l.swf?video_id=A72jSPEtOJI">< /mx:SWFLoader>
< mx:Button x="688" y="505" label="1st" click="loadSWF('http://www.youtube.com/swf/l.swf?video_id=A72jSPEtOJI')"/>
< mx:Button x="761" y="505" label="2nd" click="loadSWF('http://www.youtube.com/swf/l.swf?video_id=InY3SLWORj8')"/>


It works fine with other swf files that don't involve youtube videos... any ideas... very much appreciated!

Anonymous said...

Hello I do not agree ...