第一步:建立加载类[CtmObjLoader],此类可以加载文件类型 [图片文件或swf文件]
package
{
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.events.*;
import .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 加载的地址
//@progressHandle 加载中的处理过程
//bShow 加载后是否显示出来
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);//如果需要显示,则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(PLETE, 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(PLETE))dispatcher.removeEventListener(PLETE, 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(PLETE));
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
dispatchEvent(new Event(HTTPStatusEvent.HTTP_STATUS));
}
private function ioErrorHandler(event:IOErrorEvent):void {
dispatchEvent(new Event(IOErrorEvent.IO_ERROR));
}
}
}
第二步:建立加载[flash as3 swf文件]类
//此类专提供flash as3 swf文件加载,flex swf不需要,当然也可以写成一个类,我习惯这样
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;
}
}
}
第三步:建立flex主文件1[mainforflex.mxml]加载flex swf外部文件用
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="//mxml" layout="absolute" xmlns:ns1="ManageView.*" fontSize="12">
<mx:Script>
<![CDATA[
import Package.mySwfLoader;
/*
Flex 控制加载外部的swf文件[子文件为flex版]
*/
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自己定义去吧
}else{
_CtmObjLoader=new mySwfLoader('flexchild.swf');
_CtmObjLoader.addEventListener(IOErrorEvent.IO_ERROR,OnFailHandle);
_CtmObjLoader.addEventListener(PLETE,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为加载的文件public函数
}catch(e:Error){
Alert.show(e.message);
}
}else{
Alert.show('请加载文件');
return;
}
}
private function testAddChildListener():void{
if(!_CtmObjLoader){Alert.show('请加载文件');return;}
try{
LoadedSM.document.addEventListener("EventGoTOParent",OnTestCall);
Alert.show('设置成功');
}catch(e:Error){Alert.show(e.message)}
}
]]>
</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>
第四步:建立flex swf子文件[flexchild.mxml]
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="//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>
运行试试,加载flex swf文件完成,下面讲加载flash cs3 swf文件吧
第一步:建立flex主文件1[mainforflash.mxml]加载flash cs3 swf外部文件用
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="//mxml" layout="absolute" xmlns:ns1="ManageView.*" fontSize="12">
<mx:Script>
<![CDATA[
/*
Flex 控制加载外部的swf文件[子文件为flash cs3 版]
*/
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你自己去写了,我不太会,但我做了一个简单的,可用的
}else{
_mySwfLoader=new mySwfLoader('flashchild.swf');
_mySwfLoader.addEventListener(IOErrorEvent.IO_ERROR,OnFailHandle);
_mySwfLoader.addEventListener(PLETE,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('请加载文件');
return;
}
}
private function stop():void{
if(LoadedSM){
try{
LoadedSM.stop();
}catch(e:Error){
Alert.show(e.message);
}
}else{
Alert.show('请加载文件');
return;
}
}
private function play():void{
if(LoadedSM){
try{
LoadedSM.play();
}catch(e:Error){
Alert.show(e.message);
}
}else{
Alert.show('请加载文件');
return;
}
}
private function testAddChildListener():void{
if(!_mySwfLoader){Alert.show('请加载文件');return;}
var d:DisplayObject=_mySwfLoader.content;
try{
LoadedSM.addEventListener("EventGoTOParent",OnTestCall);
Alert.show('设置成功');
}catch(e:Error){Alert.show(e.message)}
//_Url=urltxt.text;
}
]]>
</mx:Script>
<mx:Button label="加载flash子文件" click="LoadSwf();" x="26" y="10"/>
<mx:Button label="为加载的文件设置Listener" click="testAddChildListener();" x="160" y="10"/>
<mx:Button label="测试加载的文件函数gotoAndPlay(0)" click="gotoAndPlay(int(frametxt.text));" x="351" y="10"/>
<mx:TextInput id="frametxt" x="623" y="10" width="63"/>
</mx:Application>
第二步:写flash cs3 文件,这个不在行,我写了个简单的测试,通过 的,所以你就自己写吧
可以调用flash cs3 文件的函数,也可以监听他发来的事件
-----End-------如果看到写法有问题,请回帖告知,谢谢了,