This is a prototype to load external flash9 swf file into flex and using the library items from the swf. In given example we have two library symbols with linkage ids (”mc1″, “mc2″).
Just make sure when you create a movieclip from these definitions, you firstly add these to a UIComponent and then that component to stage or any container, otherwise there will be a conversion error you will be getting
You can download sources from here
Following is the code for the same.
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” creationComplete=”init()” layout=”absolute”>
<mx:Script>
<![CDATA[
import mx.core.UIComponent;
//-------- Declare Variables --------
private var swfLoader:Loader;
private var fileURL:URLRequest;
//----- Initialize Function ------------
private function init():void{
// Security.allowDomain("*");
swfLoader = new Loader();
_fileURL = new URLRequest("sample.swf");
configureListeners(swfLoader.contentLoaderInfo);
try{
swfLoader.load(_fileURL);
}
catch(e:IOErrorEvent){
trace(e.toString());
}
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, onSWFLoaded);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(Event.INIT, initHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(Event.UNLOAD, unLoadHandler);
}
private function completeHandler(event:Event):void {
trace("completeHandler: " + event);
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
trace("httpStatusHandler: " + event);
}
private function initHandler(event:Event):void {
trace("initHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
private function openHandler(event:Event):void {
trace("openHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
trace("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}
private function unLoadHandler(event:Event):void {
trace("unLoadHandler: " + event);
}
//-----
private function onSWFLoaded(event:Event):void{
var mClip:Class = event.target.applicationDomain.getDefinition("mc1") as Class;
var mClip1:Class = event.target.applicationDomain.getDefinition("mc2") as Class;
var mClipInstance:MovieClip = new mClip() as MovieClip;
var mClipInstance1:MovieClip = new mClip1() as MovieClip;
//trace(mClipInstance.totalFrames);
var uiComp:UIComponent = new UIComponent();
uiComp.addChild(mClipInstance);
uiComp.addChild(mClipInstance1);
addChild(uiComp);
uiComp.x = 100;
uiComp.y = 100;
uiComp.getChildAt(0).x = 150;
}
]]>
</mx:Script>
</mx:Application>