[AS3]ApplicationDomain自定义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
- package
- {
- import flash.display.Loader;
- import flash.errors.IllegalOperationError;
- import flash.events.Event;
- import flash.events.EventDispatcher;
- import flash.events.IOErrorEvent;
- import flash.events.SecurityErrorEvent;
- import flash.net.URLRequest;
- import flash.system.ApplicationDomain;
- import flash.system.LoaderContext;
- public class ClassLoader extends EventDispatcher {
- public static var CLASS_LOADED:String = "classLoaded";
- public static var LOAD_ERROR:String = "loadError";
- public var loader:Loader;
- private var swfLib:String;
- private var loadedClass:Class;
- public function ClassLoader() {
- loader = new Loader();
- }
- public function load(lib:String):void {
- swfLib = lib;
- var request:URLRequest = new URLRequest(swfLib);
- var context:LoaderContext = new LoaderContext();
- context.checkPolicyFile=false;
- context.applicationDomain=ApplicationDomain.currentDomain;
- //trace(ApplicationDomain.currentDomain==ApplicationDomain.currentDomain);
- loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
- loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
- loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
- loader.load(request,context);
- }
- private function completeHandler(e:Event):void {
- loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
- loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
- loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
- dispatchEvent(new Event(ClassLoader.CLASS_LOADED));
- }
- private function ioErrorHandler(e:Event):void {
- loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
- loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
- loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
- dispatchEvent(new Event(ClassLoader.LOAD_ERROR));
- }
- private function securityErrorHandler(e:Event):void {
- loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
- loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
- loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
- dispatchEvent(new Event(ClassLoader.LOAD_ERROR));
- }
- public function hasClass(className:String):Boolean{
- return loader.contentLoaderInfo.applicationDomain.hasDefinition(className);
- }
- public function getClass(className:String):Class {
- try {
- return loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
- } catch (e:Error) {
- throw new IllegalOperationError(className + " definition not found in " + swfLib);
- }
- return null;
- }
- }
- }
第一种写法: 这种情况一切正常
- package {
- import flash.display.Sprite;
- import flash.events.Event;
- public class ApplicationDomainExample extends Sprite
- {
- private var loader:ClassLoader;
- private var loader2:ClassLoader;
- public function ApplicationDomainExample() {
- loader = new ClassLoader();
- loader.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler);
- loader.load("http://127.0.0.1/lib.swf");
- loader2 = new ClassLoader();
- loader2.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler2);
- <span style="color: #ff0000;">/* 两个loader 一起load情况正常 */
- loader2.load("http://127.0.0.1/lib2.swf");</span>
- }
- private function classLoadedHandler(e:Event):void {
- loader.removeEventListener(Event.COMPLETE,classLoadedHandler);
- getUI(loader);
- //loader2.load("http://127.0.0.1/lib2.swf"); //load任何swf文件 包括它自己
- /* 下面情况也正常 */
- //loader2.load("lib.swf");
- }
- private function classLoadedHandler2(e:Event):void {
- loader2.removeEventListener(Event.COMPLETE,classLoadedHandler2);
- getUI(loader);
- }
- private function getUI(l:ClassLoader):void{
- if(l.hasClass("Lib3")){
- var ui:Object=l.getClass("Lib3");
- trace(new ui);
- }
- }
- }
- }
第二种写法: 正常
- package {
- import flash.display.Sprite;
- import flash.events.Event;
- public class ApplicationDomainExample extends Sprite
- {
- private var loader:ClassLoader;
- private var loader2:ClassLoader;
- public function ApplicationDomainExample() {
- loader = new ClassLoader();
- loader.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler);
- loader.load("http://127.0.0.1/lib.swf");
- loader2 = new ClassLoader();
- loader2.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler2);
- /* 下面情况正常 */
- //loader2.load("http://127.0.0.1/lib2.swf");
- }
- private function classLoadedHandler(e:Event):void {
- loader.removeEventListener(Event.COMPLETE,classLoadedHandler);
- getUI(loader);
- //loader2.load("http://127.0.0.1/lib2.swf"); //load任何swf文件 包括它自己
- /* 下面情况也正常 */
- <span style="color: #ff0000;">loader2.load("lib.swf"); //这种跟自己是同域所以正常 可以理解</span>
- }
- private function classLoadedHandler2(e:Event):void {
- loader2.removeEventListener(Event.COMPLETE,classLoadedHandler2);
- getUI(loader);
- }
- private function getUI(l:ClassLoader):void{
- if(l.hasClass("Lib3")){
- var ui:Object=l.getClass("Lib3");
- trace(new ui);
- }
- }
- }
- }
第三种写法:报错
- package {
- import flash.display.Sprite;
- import flash.events.Event;
- public class ApplicationDomainExample extends Sprite
- {
- private var loader:ClassLoader;
- private var loader2:ClassLoader;
- public function ApplicationDomainExample() {
- loader = new ClassLoader();
- loader.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler);
- loader.load("http://127.0.0.1/lib.swf");
- loader2 = new ClassLoader();
- loader2.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler2);
- }
- private function classLoadedHandler(e:Event):void {
- loader.removeEventListener(Event.COMPLETE,classLoadedHandler);
- getUI(loader);
- <span style="color: #ff0000;">loader2.load("http://127.0.0.1/lib2.swf"); //load任何swf文件 包括它自己 这样写就报错</span>
- }
- private function classLoadedHandler2(e:Event):void {
- loader2.removeEventListener(Event.COMPLETE,classLoadedHandler2);
- getUI(loader);
- }
- private function getUI(l:ClassLoader):void{
- if(l.hasClass("Lib3")){
- var ui:Object=l.getClass("Lib3");
- trace(new ui);
- }
- }
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例