Lately I have been working on a flex app that loads tons of swf’s. When it started, It was never designed to load other Flex apps. All was well for a time until we received the go ahead to build content as Flex Applications and I tried to dispatch events into them… the loaded swf’s were not picking up what I was laying down.

the goal here is to be able to.

loadedApp.dispatchEvent(new CustomEvent(CustomEvent.WHATEVS));

Now. If you are going to try and do this on a swf from the Flash IDE or a swf from a pure AS3 project, you don’t have to do anything than what is listed above. If you are going to try this on a loaded swf that came from a Flex app you have to get around the SystemManager first.

//class member
private var modloader:SwfLoader;
// then i have following housed in a function
modLoader = new SWFLoader();
modLoader.showBusyCursor = true;
modLoader.load(yourLoadedSwf);
modLoader.addEventListener(IOErrorEvent.IO_ERROR, hanIOError);
modLoader.addEventListener(Event.OPEN, showMonitor);
modLoader.addEventListener(Event.COMPLETE, hanComplete);
modLoader.addEventListener(ProgressEvent.PROGRESS, monitor);

None of the other listeners are really relevant to this post so I’ll just focus on the complete function. What we are doing with the following code is checking to see if the content that is being loaded into the SwfLoader is of the type SystemManager and if so, listen for the APPLICATION_COMPLETE event, when that happens cast the loaded content as an Application and store the reference in the variable “loadedModule”

private function hanComplete(e:Event):void
{
     if(modLoader.content is SystemManager){
		var sm:SystemManager = modLoader.content as SystemManager;			
		var appReady:Function = function(e:FlexEvent):void{
			var loadedApp:Application = Application(sm.application);
			loadedModule = loadedApp;
		};
					
		sm.addEventListener(FlexEvent.APPLICATION_COMPLETE, appReady);
					
		}else{
			loadedModule = modLoader.content;
		}
}

then as needed, you can.

loadedModule.dispatchEvent(new Event("OutOfTheWaySystemManager"));

Hope that helps!