flash AS3应用程序模块化开发与ApplicationDomain
|
程序越来越大,我们需要把它拆分成多个swf,在需要的时候动态加载。拆分时应该尽量把不同的类编译进唯一的swf,避免因swf文件增多而使整个程序的文件尺寸增大。按此原则可以拆分出以下两种swf,借助 ApplicationDomain 共享其代码和资源。
[Copy to clipboard] [ - ] CODE:var loader : Loader = new Loader(); var context : LoaderContext = new LoaderContext(); /* 加载到子域(模块) */ context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain); /* 加载到同域(共享库) */ context.applicationDomain = ApplicationDomain.currentDomain; /* 加载到新域(独立运行的程序或模块) */ context.applicationDomain = new ApplicationDomain(); loader.load(new URLRequest("loaded.swf"), context);
[Copy to clipboard] [ - ] CODE:this.stage.addChild(mySprite); this.addChild(myMC); this.addChild(myShape);
![]()
![]()
ApplicationDomain 的 hasDefinition() 方法判断某定义是否存在,getDefinition() 获取指定的定义。下面以一个 例子 来介绍 ApplicationDomain 的具体用法和应用程序的拆分。 [Copy to clipboard] [ - ] CODE:private function showModule(p_module : IModule) : void { if (this.m_moduleList[0] == "login.swf") { p_module.show(this); p_module.addEventListener("login", this.onLogin); } else { p_module.show(this, this.m_userName); } }
[Copy to clipboard] [ - ] CODE:protected function getClass(p_name : String) : Class { try { return ApplicationDomain.currentDomain.getDefinition(p_name) as Class; } catch (p_e : ReferenceError) { trace("定义 " + p_name + " 不存在"); return null; } return null; }
[Copy to clipboard] [ - ] CODE:private function onLogin(p_e : Object) : void { this.m_userName = p_e.userName; var login : IModule = p_e.currentTarget; login.removeEventListener("login", this.onLogin); login.dispose(); this.loadSwf(); }
[Copy to clipboard] [ - ] CODE:public function show(p_parent : DisplayObjectContainer, ... rest) : void { var libClass : Class = this.getClass("net.eidiot.appDomainDemo.Libaray"); if (libClass != null) this.initUi(libClass, rest); } override protected function initUi(p_libClass : Class, p_rest : Array = null) : void { this.addUi(this.getClass(p_libClass.BG_NAME), "结果"); var resultFunc : Function = p_libClass.getResult; var userName : String = p_rest[0]; this.addChild(resultFunc(userName)); }
|



01.gif (2.47 KB)