R – How to dynamically load a Movieclip in Flash CS4 and have it share information with a framework

actionscript-3dynamicflash

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!

Best Answer

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.

Related Topic