现在的位置: 首页 > 综合 > 正文

Flex控制外部加载的swf文件[外部swf文件flex版]

2013年08月01日 ⁄ 综合 ⁄ 共 5848字 ⁄ 字号 评论关闭

1-首先写一个加载外部swf文件的类

package Package
{

 import flash.display.DisplayObject;
 import flash.display.Loader;
 import flash.events.*;
 import flash.net.URLRequest;
 
 import mx.core.UIComponent;
 import mx.events.FlexEvent;

 public class CtmObjLoader extends UIComponent
 {
  private var loader:Loader;
  private var ProgressHandle:Function=null;
  public var _bLoaded:Boolean=false;
  public function CtmObjLoader(UrlRequest:String=null,progressHandle:Function=null)
  {
   super();
   if(UrlRequest)LoadThis(UrlRequest,progressHandle);
  }
  public function LoadThis(UrlRequest:String,progressHandle:Function=null):void{
   RemoveChild();
            loader = new Loader();
            ProgressHandle=progressHandle;
            //DeleteListeners(loader.contentLoaderInfo);
            configureListeners(loader.contentLoaderInfo);
            addChild(loader);
            var request:URLRequest = new URLRequest(UrlRequest);
            loader.load(request);   
  }
  public function UnLoadThis():void{
   try{
    RemoveChild();
    this.parent.removeChild(this);
   }catch(e:Error){}
   //this=null; 
  }
  public function get content():DisplayObject{
   if(!_bLoaded)return null;
   return loader.content;
  }
  private function RemoveChild():void{
            if(_bLoaded){
             try{
              DeleteListeners(loader.contentLoaderInfo);
              loader.unload();
              removeChild(loader);
              loader=null;
              _bLoaded=false;
              }catch(e:Error){}
            } 
  }
        private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            //var a:FlexEvent
            dispatcher.addEventListener(FlexEvent.APPLICATION_COMPLETE, tttt);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            if(ProgressHandle!=null)dispatcher.addEventListener(ProgressEvent.PROGRESS,

ProgressHandle);
                 
        }
        private function tttt(event:FlexEvent):void{
         dispatchEvent(new Event(FlexEvent.APPLICATION_COMPLETE));
        }
        private function DeleteListeners(dispatcher:IEventDispatcher):void {
            if(dispatcher.hasEventListener(Event.COMPLETE))dispatcher.removeEventListener

(Event.COMPLETE, completeHandler);
            if(dispatcher.hasEventListener(HTTPStatusEvent.HTTP_STATUS))

dispatcher.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            if(dispatcher.hasEventListener(IOErrorEvent.IO_ERROR))

dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            if(dispatcher.hasEventListener(ProgressEvent.PROGRESS))
             if(ProgressHandle!=null)dispatcher.removeEventListener(ProgressEvent.PROGRESS,

ProgressHandle);        
        }
        private function completeHandler(event:Event):void {
         this.width=loader.content.width;
         this.height=loader.content.height;
         _bLoaded=true;        
         dispatchEvent(new Event(Event.COMPLETE));
         //if(CmpHandle)CmpHandle
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void {
         dispatchEvent(new Event(HTTPStatusEvent.HTTP_STATUS));
        }
        private function ioErrorHandler(event:IOErrorEvent):void {
   dispatchEvent(new Event(IOErrorEvent.IO_ERROR));
        }

 }
}

这个类用来加载外部swf文件[flex-flash],当然也可以加载图片文件

 

 

2.写主文件main.mxml

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

xmlns:ns1="ManageView.*" fontSize="12">
 <mx:Script>
  <![CDATA[
   /*
   Flex 控制加载外部的swf文件[子文件为flex版]
   */
   import mx.managers.SystemManager;
   import mx.controls.Alert;
   import Package.CtmObjLoader;
   import flash.utils.setTimeout;
   [Bindable]private var _CtmObjLoader:CtmObjLoader;
   private var LoadedSM:SystemManager;
   private function LoadSwf():void{//加载文件函数
    LoadedSM=null;//子文件管理器
    if(_CtmObjLoader){
     _CtmObjLoader.LoadThis(''child.swf'');//child.swf为子文件名称,同级目录
    }else{
   
     _CtmObjLoader=new CtmObjLoader(''child.swf'');
     _CtmObjLoader.addEventListener(IOErrorEvent.IO_ERROR,OnFailHandle);//监听失败事件
     _CtmObjLoader.addEventListener(Event.COMPLETE,OnCompleteHandle);//监听加载完成事件
     this.addChild(_CtmObjLoader);//将加载容器加入主程序

    }
    _CtmObjLoader.setStyle(''horizontalCenter'',0);
    _CtmObjLoader.setStyle(''verticalCenter'',0);   
   }
   private function OnFailHandle(e:Event):void{
    mx.controls.Alert.show(''error'');
   }
   private function OnCompleteHandle(E:Event):void{
    LoadedSM=SystemManager(_CtmObjLoader.content);
   } 

   private function OnTestCall(e:Event):void{//响映子文件事件
    Alert.show(''Child Event Call'');
   }  
   private function MCtestCall():void{//调用子文件函数
    if(LoadedSM){
     try{
      LoadedSM.document.TestCall(''From Parent Call'');
     
     }catch(e:Error){
      Alert.show(e.message);
     }
    }else{
     Alert.show(''请加载文件'');
     return;
    }
   }
   private function testAddChildListener():void{//为子文件设置回调事件,如不设置,将收不到子

文件事件响映
    if(!_CtmObjLoader){Alert.show(''请加载文件'');return;}
    var d:DisplayObject=_CtmObjLoader.content;
    try{
     LoadedSM.document.addEventListener("EventGoTOParent",OnTestCall);
     Alert.show(''设置成功'');
    }catch(e:Error){Alert.show(e.message)}
    //_Url=urltxt.text;
   }
  ]]>
 </mx:Script>
 <mx:Button label="加载flex子文件" click="LoadSwf();" x="26" y="10"/>
 <mx:Button label="为加载的文件设置Listener" click="testAddChildListener();" x="150" y="10"/>
 <mx:Button label="测试加载的文件函数" click="MCtestCall();" x="342" y="10"/>
</mx:Application>
3.写子文件child.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="750" height="444"
  applicationComplete="initApp()" styleName="Border3" fontSize="12">
<mx:Script>
 <![CDATA[
  import mx.controls.Alert;

  private function initApp():void{
 
  }
  public function TestCall(msg:String):void{
   mx.controls.Alert.show(msg);
  }
  private function EventGoToParent():void{
   dispatchEvent(new Event(''EventGoTOParent''));
  }
   
 ]]>
</mx:Script>
 <mx:Button x="327" y="148" label="向Parent通知事件" click="EventGoToParent()"/>

 
</mx:Application>
完成,文件都可以直接copy后编译运行

 

抱歉!评论已关闭.