`
ejhmm20u
  • 浏览: 13047 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

SWF加载和调用另一个SWF中的类中函数

阅读更多

SWF加载和调用另一个SWF中的类中函数
2010年12月27日
  AS技术2010-12-27 16:19:38阅读0评论0  字号:大中小 订阅 /*
  * SWF加载
  */
  package
  {
  import flash.display.Loader;
  import flash.display.LoaderInfo;
  import flash.events.Event;
  import flash.events.EventDispatcher;
  import flash.events.IOErrorEvent;
  import flash.events.ProgressEvent;
  import flash.events.SecurityErrorEvent;
  import flash.net.URLRequest;
  import flash.system.ApplicationDomain;
  import flash.system.LoaderContext;
  /**
  * SWF加载器
  */
  public class SWFLoader extends EventDispatcher
  {
  private var m_loadinfo:LoaderInfo;
  public function get loadinfo():LoaderInfo
  {
  return m_loadinfo;
  }
  public function SWFLoader() {}
  /**
  加载SWF
  */
  public function Load(url:String):void
  {
  var loader:Loader         = new Loader();
  var context:LoaderContext = new LoaderContext();
  /** 加载到子域 */
  context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain) ;
  InitLoadEvent(loader.contentLoaderInfo);
  loader.load(new URLRequest(url),context);
  }
  /**
  * 获取当前ApplicationDomain内的类定义
  * 
  * name类名称,必须包含完整的命名空间,如 Grave.Function.SWFLoader
  * info加载swf的LoadInfo,不指定则从当前域获取
  * return获取的类定义,如果不存在返回null
  */
  public function GetClass(name:String, info:LoaderInfo = null):Class 
  {
  try 
  {
  if (info == null)
  {
  return ApplicationDomain.currentDomain.getDefinition(name ) as Class;
  }
  return info.applicationDomain.getDefinition(name) as Class;
  }
  catch (e:ReferenceError)
  {
  //trace("定义 " + name + " 不存在");
  return null;
  }
  return null;
  }
  /**
  * @private
  * 监听加载事件
  * 
  * @param info加载对象的LoaderInfo
  */
  private function InitLoadEvent(info : LoaderInfo):void 
  {
  info.addEventListener(ProgressEvent.PROGRESS, this.onProgress);
  info.addEventListener(Event.COMPLETE, this.onComplete);
  info.addEventListener(IOErrorEvent.IO_ERROR, this.onError);
  info.addEventListener(SecurityErrorEvent.SECURITY_ ERROR, this.onError);
  }
  /**
  * @private
  * 移除加载事件
  * 
  * @param inft加载对象的LoaderInfo
  */
  private function RemoveLoadEvent(info:LoaderInfo):void 
  {
  info.removeEventListener(Event.COMPLETE,onComplete );
  info.removeEventListener(ProgressEvent.PROGRESS,on Progress);
  info.removeEventListener(IOErrorEvent.IO_ERROR,onE rror);
  info.removeEventListener(SecurityErrorEvent.SECURI TY_ERROR, onError);
  }
  ///////加载事件
  private function onComplete(e:Event):void 
  {
  var info:LoaderInfo = e.currentTarget as LoaderInfo;
  RemoveLoadEvent(info);
  m_loadinfo = info;
  this.dispatchEvent(e);
  }
  ////// 加载中 
  private function onProgress(e:ProgressEvent):void 
  {
  this.dispatchEvent(e);
  }
  ///////////// 出错事件
  private function onError(e:Event):void
  {
  this.dispatchEvent(e);
  }
  }
  }
  /////////////////////////////////////主类的调用
  package  
  {
  import flash.display.Sprite;
  import flash.events.Event;
  /**
  * ...
  * @author 
  */
  public class Test extends Sprite
  {
  private var swfload:SWFLoader;
  public function Test() 
  {
  start();
  }
  private function start():void
  {
  swfload = new SWFLoader();
  swfload.Load("Button/Button.swf");
  swfload.addEventListener(Event.COMPLETE, evt);
  }
  private function evt(e:Event):void
  {
  var Tempclass:Class = swfload.GetClass("MC", swfload.loadinfo);
  var button_1:*= new Tempclass();
  button_1.x = 275;
  button_1.y = 180;
  addChild(button_1);
  button_1.click1();
  var button_2:*= new Tempclass();
  button_2.x = 275;
  button_2.y = 250;
  addChild(button_2);
  button_2.click2();
  }
  }
  }
  /////////////////////////另一个SWF的一个元件类
  package
  {
  import flash.display.Sprite;
  import flash.events.MouseEvent;
  /**
  * ...
  * @author 
  */
  public class MC extends Sprite
  {
  public function MC() 
  {
  }
  public function click():void
  {
  }
  public function click1():void
  {
  Btn.addEventListener(MouseEvent.CLICK, mouse1);
  }
  private function mouse1(e:MouseEvent):void
  {
  trace("同一按钮的触发事件1……");
  }
  public function click2():void
  {
  Btn.addEventListener(MouseEvent.CLICK, mouse2);
  }
  private function mouse2(e:MouseEvent):void
  {
  trace("同一按钮的触发事件2……");
  }
  }
  } 
  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics