flex load external swf and communicate with each other

2010-12-10  来源:本站原创  分类:Flash  人气:149 

Reprinted from
http://wenchao445431480.blog.163.com/blog/static/181088292009109114921899/

The first step: the establishment of load class [CtmObjLoader], these can load the file types [picture files or swf files]

package
{ 

import flash.display.DisplayObject;
import flash.display.Loader;
import flash.events.*;
import flash.net.URLRequest; 

import mx.core.UIComponent; 

public class CtmObjLoader extends UIComponent
{
  private var ProgressHandle:Function=null;
  public var loader:Loader;
  public var _bLoaded:Boolean=false;
  public var _bShow:Boolean=true;
  //@UrlRequest   The address of the load
  //@progressHandle  Loading process
  //bShow   Loaded is displayed
  public function CtmObjLoader(UrlRequest:String=null,progressHandle:Function=null,bShow:Boolean=true)
  {
   super();
   if(UrlRequest)LoadThis(UrlRequest,progressHandle,bShow);
  }
  public function LoadThis(UrlRequest:String,progressHandle:Function=null,bShow:Boolean=true):void{
   RemoveChild();
            loader = new Loader();
            _bShow=bShow;
            ProgressHandle=progressHandle;
            configureListeners(loader.contentLoaderInfo);
            if(_bShow)addChild(loader);// If you want to display, then addchild
            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;
  }
  public function RemoveChild():void{
            if(_bLoaded){
             try{
              DeleteListeners(loader.contentLoaderInfo);
              loader.unload();
              if(_bShow)removeChild(loader);
              loader=null;
              _bLoaded=false;
             }catch(e:Error){throw new Error('Define ObjLoader Remove Error')}
            }
  }
        private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            if(ProgressHandle!=null)dispatcher.addEventListener(ProgressEvent.PROGRESS, ProgressHandle); 

        }
        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));
        } 

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

}
}

Step Two: Create a load [flash as3 swf file] class

// This class is designed to provide flash as3 swf file loading ,flex swf Need not, of course, can also be written as a class , I used this
package
{
import flash.display.MovieClip; 

public class mySwfLoader extends CtmObjLoader
{
  public function mySwfLoader(UrlRequest:String=null, progressHandle:Function=null, bShow:Boolean=true)
  { 

   super(UrlRequest, progressHandle, bShow);
  }
  public function get movieClip():MovieClip{
   if(!_bLoaded)return null
   return content as MovieClip;
  } 

}
}

The third step: the establishment of flex master file 1 [mainforflex.mxml] load flex swf file with an external

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="ManageView.*" fontSize="12">
<mx:Script>
  <![CDATA[
   import Package.mySwfLoader;
   /*
   Flex  Control of loading external SWF files [ Sub file for flex version ]
   */
   import mx.managers.SystemManager;
   import mx.controls.Alert;
   import Package.CtmObjLoader;
   import flash.utils.setTimeout;
   [Bindable]private var _CtmObjLoader:mySwfLoader;
   private var LoadedSM:SystemManager;
   private var loadedMoiveClip:MovieClip;
   private function LoadSwf():void{
    LoadedSM=null;
    loadedMoiveClip=null;
    if(_CtmObjLoader){
     _CtmObjLoader.LoadThis('flexchild.swf');//child.swf Custom go
    }else{ 

     _CtmObjLoader=new mySwfLoader('flexchild.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');//TestCall To load the file public function  

     }catch(e:Error){
      Alert.show(e.message);
     }
    }else{
     Alert.show(' Please load the file ');
     return;
    }
   }
   private function testAddChildListener():void{
    if(!_CtmObjLoader){Alert.show(' Please load the file ');return;}
    try{
     LoadedSM.document.addEventListener("EventGoTOParent",OnTestCall);
     Alert.show(' Set successfully ');
    }catch(e:Error){Alert.show(e.message)}
   }
  ]]>
</mx:Script>
<mx:Button label=" Add flex child file " click="LoadSwf();" x="26" y="10"/>
<mx:Button label=" To load the file settings Listener" click="testAddChildListener();" x="150" y="10"/>
<mx:Button label=" Test load file function " click="MCtestCall();" x="342" y="10"/>
</mx:Application>

Step Four: Create a sub-flex swf file [flexchild.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=" Notification events to the Parent " click="EventGoToParent()"/>
</mx:Application>

Try running, load flex swf file has finished, the following load flash cs3 swf file about it

Step five : Build flex master file 1[mainforflash.mxml] Load external swf files with flash
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="ManageView.*" fontSize="12">
<mx:Script>
  <![CDATA[
   /*
   Flex  Control of loading external SWF files [ Sub version for flash cs3 document ]
   */
   import mx.controls.Alert;
   import Package.mySwfLoader;
   import flash.utils.setTimeout;
   [Bindable]private var _mySwfLoader:mySwfLoader;
   private var LoadedSM:MovieClip;
   private function LoadSwf():void{
    if(_mySwfLoader){
     _mySwfLoader.LoadThis('flashchild.swf');//flashchild.swf You wrote it yourself, I'm not going to , But I had a simple, free of
    }else{ 

     _mySwfLoader=new mySwfLoader('flashchild.swf');
     _mySwfLoader.addEventListener(IOErrorEvent.IO_ERROR,OnFailHandle);
     _mySwfLoader.addEventListener(Event.COMPLETE,OnCompleteHandle);
     this.addChild(_mySwfLoader); 

    }
    _mySwfLoader.setStyle('horizontalCenter',0);
    _mySwfLoader.setStyle('verticalCenter',0);
   }
   private function OnFailHandle(e:Event):void{
    mx.controls.Alert.show('error');
   }
   private function OnCompleteHandle(E:Event):void{
    if(_mySwfLoader.content)Alert.show('ok0');
    try{
     LoadedSM=_mySwfLoader.movieClip;
    }catch(e:Error){Alert.show(e.message)}
   }
   private function OnTestCall(e:Event):void{
    Alert.show('Child Event Call');
   }
   private function gotoAndPlay(frame:int=0):void{
    //(_CtmObjLoader.loader.content as MovieClip).
    if(LoadedSM){
     try{
      LoadedSM.gotoAndPlay(frame); 

     }catch(e:Error){
      Alert.show(e.message);
     }
    }else{
     Alert.show(' Please load the file ');
     return;
    }
   }
   private function stop():void{
    if(LoadedSM){
     try{
      LoadedSM.stop(); 

     }catch(e:Error){
      Alert.show(e.message);
     }
    }else{
     Alert.show(' Please load the file ');
     return;
    }
   }
   private function play():void{
    if(LoadedSM){
     try{
      LoadedSM.play(); 

     }catch(e:Error){
      Alert.show(e.message);
     }
    }else{
     Alert.show(' Please load the file ');
     return;
    }
   }
   private function testAddChildListener():void{
    if(!_mySwfLoader){Alert.show(' Please load the file ');return;}
    var d:DisplayObject=_mySwfLoader.content;
    try{
     LoadedSM.addEventListener("EventGoTOParent",OnTestCall);
     Alert.show(' Set successfully ');
    }catch(e:Error){Alert.show(e.message)}
    //_Url=urltxt.text;
   }
  ]]>
</mx:Script>
<mx:Button label=" Loading flash sub-file " click="LoadSwf();" x="26" y="10"/>
<mx:Button label=" To load the file settings Listener" click="testAddChildListener();" x="160" y="10"/>
<mx:Button label=" Test load file function gotoAndPlay(0)" click="gotoAndPlay(int(frametxt.text));" x="351" y="10"/>
<mx:TextInput x="623" y="10" width="63"/>
</mx:Application>
相关文章
  • flex load external swf and communicate with each other 2010-12-10

    Reprinted from http://wenchao445431480.blog.163.com/blog/static/181088292009109114921899/ The first step: the establishment of load class [CtmObjLoader], these can load the file types [picture files or swf files] package { import flash.display.Displa

  • Load external SWF files 2010-05-22

    Load external SWF files package mytest.dt { import flash.display.Loader; import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.net.URLRequest; import flash.system.Applicat

  • Loader's direct use of air in the loadBytes () method to load the swf to be wrong 2010-11-02

    Because the swf is an executable file, so aodbe air Binary files loaded by the way URLLoader to load a swf into memory the outside, this time if not addressed directly through the Loader instance loadBytes () method to load this memory The ByteArray

  • Several points of Flex load swf 2010-04-07

    Today, Flash and Flex3 tossing most of the day co-ordination of special sum up their feelings and experiences: 1) If it is to embed the swf through Embed then, Flex3 only support FlashCS2 created swf. 2) If yes, then through the loader to load only t

  • flex import external libraries 2008-06-27

    One, you can use the command-line MXML compiler (amxmlc) to compile AIR applications Adobe ® ActionScript ® 3.0 and MXML resources: amxmlc [compiler options] -- MyAIRApp.mxml Where [compiler options] specifies compiled AIR application using the comma

  • Loading loader (How to Load External Images in Actionscript 3.0) 2010-05-28

    Using the flash.display.Loader Class in AS3, you can load in external image Files to Display in Flash. The loader supports Class Loading in JPG, SWF, PNG, or GIF File types. The Loader Class sounds more like a Loading Manager that watches load progre

  • Load loader (How to Load External Images in Actionscript 3.0) 2010-05-28

    Using the flash.display.Loader Class in AS3, you can load in external image Files to Display in Flash. The loader supports Class Loading in JPG, SWF, PNG, or GIF File types. The Loader Class sounds more like a Loading Manager that watches load progre

  • Flex load pictures, menu bar, editor, directory tree, picture upload, players, 2010-04-15

    1, loading images or swf files <? Xml version = "1.0" encoding = "utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"> / / Auto load i

  • flex (actionscript) with swf dynamic library (dll) 2010-05-28

    In the following code, a class is given public Class property circleClass, while the Class attributes can be the main Library class as a property to be quoted: package { import flash.display.Sprite; public class Library extends Sprite { public var ci

  • FontLoader 2 external swf called font embedding class 2010-11-02

    If you want the dynamic text on the SWF in the application of non-system fonts, of course is embedded in the SWF itself the corresponding font, another font is embedded in the SWF to another, when the need for the corresponding font, this SWF to load

  • Loading how to do a custom load other swf 2011-05-30

    const FILE_PATH:String="main.swf"; const CLASS_NAME:String="MainSwf"; var loader:Loader; var request:URLRequest; loader = new Loader(); request=new URLRequest(FILE_PATH); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompl

  • flex load the txt file URLLoader 2010-12-30

    Click the button to perform btn_downloadModel_clickHandler: you can access the contents of a text file protected function btn_downloadModel_clickHandler (event: MouseEvent): void { / * @ Get Data from Server * / var src: String = " http://localhost:8

  • Flex swc swz swf RSL 2010-02-11

    Description swc production, RSL's production process. http://www.360doc.com/content/08/1121/18/561_1973334.shtml http://blog.chinaunix.net/u/21752/showart_2091925.html

  • flex load picture issues related to 2010-07-06

    Desktop Background Layout Tile middle stretch DesktopBackgroundLayout LayoutManager.CENTER LayoutManager.TILE LayoutManager.STRETCH The focus is on flash.display.Graphics beginBitmapFill (bitmap: BitmapData, matrix: Matrix = null, repeat: Boolean = t

  • flex load multiple module appears empty object reference problem 2010-12-17

    Background Requirements: Tree menu by loading a number of different module. Problem phenomenon: module page there is drag, Popup, Alert or colorpicker error message: TypeError: Error # 1034: type coercion failed: can not mx.managers:: PopUpManagerImp

  • Flex runtime dynamically load swf (change) 2010-09-24

    Load external SWF files, and read one of the components and classes. Which draws in part fluorida code. //ActionScript code public class SWFLoaderWrapper { private var _loader : SWFLoader; public function SWFLoaderWrapper(swfLoader : SWFLoader) { _lo

  • Flex runtime dynamically load swf (rpm) 2010-09-24

    Load external SWF files, and read one of the components and classes. Which draws in part fluorida code. //ActionScript code public class SWFLoaderWrapper { private var _loader : SWFLoader; public function SWFLoaderWrapper(swfLoader : SWFLoader) { _lo

  • [Original] flex control of flash components 2010-02-24

    Recent agreement with the background of the hessian, communication technology and interface has been basically almost started fighting the enemy with the prospects of cooperation. Front of the flash device to be able to make it more convenient to use

  • [To] Flash for its progress three methods of loading swf 2011-01-01

    http://www.eb163.com/club/thread-16229-1-1.html The pale face of the screen allows users to tens of seconds, that's a very terrible thing. So a good flash work (games Ye Hao, animation, good strike), need a good start! And this good start is Loading.

  • "RSL Error 1 of 1" problem analysis 2010-06-09

    Because my blog was my reaction to such a question, so the Internet search, I reason. Recently to participate in a virtual community project, I produced one of the farms, games and video conferencing application modules, the entire project with multi