views:

1193

answers:

1

I'm looking to make a project in Flash that can dynamically load and unload other SWF files, where a SWF file can loaded, and a Movieclip inside can send information to the framework, and the framework can send information to the Movieclip.

Thanks!

+1  A: 

To load the the swf files you use the Loader class. Then to pass variables to the loaded swf file you have to create a setter in the documentclass of that swf file. Here's an simple example:

//This is inside the loaded swf document class
//____________________________________________________________

public function set testVar(newValue:String):void{
    trace(newValue); //This is a setter function which you use to pass parameters into.
}

public function get testVar():String{
   //Here you pass back a variable you want to be able to fetch in the mian class
   return "";
}

//This is inside of the document class
//___________________________________________________________

public function Main():void{
    var loader:Loader = new Loader(); //Create the loader
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingComplete);
    loader.load(new URLRequest(fileURL)); //Load the file
}

private function loadingComplete(event:Event):void{ 
    var loadedSwf = event.target.content; //Get the loaded swf
    addChild(loadedSwf); //Add it to the display list if you want to display it
    loadedSwf.testVar = "I'm testing!"; //This is how you use the setter function
}

As you can see, you load the file, instansiate it and then pass a variable into it. Now to the other part how to pass variables back I would probably use an event to pass it back. So you have an event listener in the main document class which listens for an event in the loaded swf. When an event is dispatchen the main class either does something or retrives a variable or sets a variable in the loaded swf with the getter or setter function. How to set up a event dispatch is pretty straight forward.

I hope I made some sense. Otherwise just comment and I'll try to explain.

Ancide
One thing to note is that if you want to actually load classes that the parent can use you'll need to look into ApplicationDomain. Using ApplicationDomain you can specify how the classes of the two SWFs will mix together.
Branden Hall
Yes. For people who doesn't know what Branden is refering to; To load a class which is within a swf's library you have to do the following: var myClass = event.target.applicationDomain.getDefinition("ClassName") as Class;myClass = new myClass();
Ancide