·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]ApplicationDomain自定义Class

时间:2014-01-23 21:50iteye.com
flash 通过ApplicationDomain 的getDefinition获取Class初始化问题, 强制转换类型失败 这样的错误

最近开发一款flash游戏,由于资源比较多,所以在个别flash中使用了flash的ApplicationDomain 的getDefinition获取Class这种方式来处理。发现由于loader写法不同 ,用loader第一次getDefinition 一切正常,第二次hasDefinition为true但getDefinition会出现TypeError: Error #1034: 强制转换类型失败 这样的错误。实在觉得奇怪。

下面的lib资源文件见附件

lib资源文件里有3个元件(影片剪辑) 分别为Lib1,Lib2,Lib3 ,分别导出类 Lib1,Lib2,Lib3。Lib3元件中有Lib1,Lib2 个一个实例。

用getDefinition方法获取的Class   Lib3  在new 的时候在第三种写法时出现错误

一下代码用FlexBuilder debug

ClassLoader.as

  1. package 
  2.     import flash.display.Loader; 
  3.     import flash.errors.IllegalOperationError; 
  4.     import flash.events.Event; 
  5.     import flash.events.EventDispatcher; 
  6.     import flash.events.IOErrorEvent; 
  7.     import flash.events.SecurityErrorEvent; 
  8.     import flash.net.URLRequest; 
  9.     import flash.system.ApplicationDomain; 
  10.     import flash.system.LoaderContext; 
  11.  
  12.     public class ClassLoader extends EventDispatcher { 
  13.         public static var CLASS_LOADED:String = "classLoaded"
  14.         public static var LOAD_ERROR:String = "loadError"
  15.         public var loader:Loader; 
  16.         private var swfLib:String; 
  17.         private var loadedClass:Class; 
  18.         public function ClassLoader() { 
  19.             loader = new Loader(); 
  20.         } 
  21.         public function load(lib:String):void { 
  22.             swfLib = lib
  23.             var request:URLRequest = new URLRequest(swfLib); 
  24.             var context:LoaderContext = new LoaderContext(); 
  25.             context.checkPolicyFile=false
  26.             context.applicationDomain=ApplicationDomain.currentDomain; 
  27.             //trace(ApplicationDomain.currentDomain==ApplicationDomain.currentDomain); 
  28.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler); 
  29.             loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler); 
  30.             loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler); 
  31.             loader.load(request,context); 
  32.         } 
  33.         private function completeHandler(e:Event):void { 
  34.             loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler); 
  35.                 loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler); 
  36.                 loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler); 
  37.                 dispatchEvent(new Event(ClassLoader.CLASS_LOADED)); 
  38.         } 
  39.      
  40.         private function ioErrorHandler(e:Event):void { 
  41.             loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler); 
  42.                 loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler); 
  43.                 loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler); 
  44.                 dispatchEvent(new Event(ClassLoader.LOAD_ERROR)); 
  45.         } 
  46.      
  47.         private function securityErrorHandler(e:Event):void { 
  48.             loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler); 
  49.                 loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler); 
  50.                 loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler); 
  51.                 dispatchEvent(new Event(ClassLoader.LOAD_ERROR)); 
  52.         } 
  53.         public function hasClass(className:String):Boolean{ 
  54.             return loader.contentLoaderInfo.applicationDomain.hasDefinition(className); 
  55.         } 
  56.         public function getClass(className:String):Class { 
  57.             try { 
  58.                 return loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class; 
  59.             } catch (e:Error) { 
  60.                 throw new IllegalOperationError(className + " definition not found in " + swfLib); 
  61.             } 
  62.             return null; 
  63.         } 
  64.     } 

第一种写法: 这种情况一切正常

  1. package { 
  2.     import flash.display.Sprite; 
  3.     import flash.events.Event; 
  4.  
  5.     public class ApplicationDomainExample extends Sprite 
  6.     { 
  7.         private var loader:ClassLoader; 
  8.         private var loader2:ClassLoader; 
  9.                 public function ApplicationDomainExample() { 
  10.                     loader = new ClassLoader(); 
  11.                     loader.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler); 
  12.                 loader.load("http://127.0.0.1/lib.swf"); 
  13.                     loader2 = new ClassLoader(); 
  14.                     loader2.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler2); 
  15.                     <span style="color: #ff0000;">/* 两个loader 一起load情况正常  */ 
  16.                     loader2.load("http://127.0.0.1/lib2.swf");</span> 
  17.         } 
  18.         private function classLoadedHandler(e:Event):void { 
  19.             loader.removeEventListener(Event.COMPLETE,classLoadedHandler); 
  20.             getUI(loader); 
  21.             //loader2.load("http://127.0.0.1/lib2.swf");  //load任何swf文件 包括它自己 
  22.             /* 下面情况也正常  */ 
  23.             //loader2.load("lib.swf"); 
  24.         } 
  25.          
  26.         private function classLoadedHandler2(e:Event):void { 
  27.             loader2.removeEventListener(Event.COMPLETE,classLoadedHandler2); 
  28.             getUI(loader); 
  29.         } 
  30.         private function getUI(l:ClassLoader):void{ 
  31.              if(l.hasClass("Lib3")){ 
  32.                 var ui:Object=l.getClass("Lib3"); 
  33.                 trace(new ui); 
  34.             }  
  35.          } 
  36.     } 

第二种写法: 正常

  1. package { 
  2.     import flash.display.Sprite; 
  3.     import flash.events.Event; 
  4.  
  5.     public class ApplicationDomainExample extends Sprite 
  6.     { 
  7.         private var loader:ClassLoader; 
  8.         private var loader2:ClassLoader; 
  9.                 public function ApplicationDomainExample() { 
  10.                     loader = new ClassLoader(); 
  11.                     loader.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler); 
  12.                     loader.load("http://127.0.0.1/lib.swf"); 
  13.                     loader2 = new ClassLoader(); 
  14.                 loader2.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler2); 
  15.                     /* 下面情况正常 */ 
  16.                     //loader2.load("http://127.0.0.1/lib2.swf"); 
  17.         } 
  18.         private function classLoadedHandler(e:Event):void { 
  19.             loader.removeEventListener(Event.COMPLETE,classLoadedHandler); 
  20.             getUI(loader); 
  21.             //loader2.load("http://127.0.0.1/lib2.swf");  //load任何swf文件 包括它自己 
  22.             /* 下面情况也正常  */ 
  23.             <span style="color: #ff0000;">loader2.load("lib.swf");     //这种跟自己是同域所以正常 可以理解</span> 
  24.         } 
  25.          
  26.         private function classLoadedHandler2(e:Event):void { 
  27.             loader2.removeEventListener(Event.COMPLETE,classLoadedHandler2); 
  28.             getUI(loader); 
  29.         } 
  30.         private function getUI(l:ClassLoader):void{ 
  31.              if(l.hasClass("Lib3")){ 
  32.                 var ui:Object=l.getClass("Lib3"); 
  33.                 trace(new ui); 
  34.             }  
  35.         } 
  36.     } 

第三种写法:报错

  1. package { 
  2.     import flash.display.Sprite; 
  3.     import flash.events.Event; 
  4.  
  5.     public class ApplicationDomainExample extends Sprite 
  6.     { 
  7.         private var loader:ClassLoader; 
  8.         private var loader2:ClassLoader; 
  9.                 public function ApplicationDomainExample() { 
  10.                     loader = new ClassLoader(); 
  11.                     loader.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler); 
  12.                     loader.load("http://127.0.0.1/lib.swf"); 
  13.                     loader2 = new ClassLoader(); 
  14.                     loader2.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler2); 
  15.         } 
  16.         private function classLoadedHandler(e:Event):void { 
  17.             loader.removeEventListener(Event.COMPLETE,classLoadedHandler); 
  18.             getUI(loader); 
  19.             <span style="color: #ff0000;">loader2.load("http://127.0.0.1/lib2.swf");  //load任何swf文件 包括它自己 这样写就报错</span> 
  20.          } 
  21.          
  22.         private function classLoadedHandler2(e:Event):void { 
  23.             loader2.removeEventListener(Event.COMPLETE,classLoadedHandler2); 
  24.             getUI(loader); 
  25.         } 
  26.         private function getUI(l:ClassLoader):void{ 
  27.              if(l.hasClass("Lib3")){ 
  28.                 var ui:Object=l.getClass("Lib3"); 
  29.                 trace(new ui); 
  30.             }  
  31.         } 
  32.     } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器