`
NewTamato
  • 浏览: 101093 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

转 flash applicationDomain

阅读更多

当程序越来越大,我们需要把它拆分成多个swf,在需要的时候动态加载。拆分时应该尽量把不同的类编译进唯一的swf,避免因swf文件增多而使整个程序的文件尺寸增大。按此原则可以拆分出以下两种swf,借助 ApplicationDomain 共享其代码和资源。

  • 模块(Module)
    按照程序逻辑,可以拆分出多个“功能模块”,如“注册”、“管理”等等;按照游戏或社区类程序的关卡或场景,可以拆分出不同的“场景模块”。这些模块不是主程序运行必须的,只在需要的时候加载。
  • 运行时共享库(RSL)
    主场景或者多个模块通用的资源,比如位图、声音、设计好的页面元素等,可作为“库”在主程序运行前加载。可以整套更换的皮肤(skin)只需先加载一套。

  ApplicationDomain 是存放AS3定义(包括类、方法、接口等)的容器。使用Loader类加载swf时可以通过指定 ApplicationDomain 参数将swf加载到不同的域(Domain):

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 ) ;

   ApplicationDomain使用类似于显示列表(DisplayList)的树形结构。 相对于舞台(Stage) ,可以认为 ApplicationDomain 最根部的是系统域(system domain),包含 Flash Player 核心类定义。主程序所在的域(以下简称主域)就是它唯一的子域,类似于Stage下的文档类(Document Class)。
  一个fla文档类里代码:

this . stage . addChild ( mySprite ) ;
this . addChild ( myMC ) ;
this . addChild ( myShape ) ;
运行后的显示列表:
饿
ApplicationDomain 的类似结构:
2
  • 加载到子域(模块)
    类似于“继承”,子域可以直接获得父域所有的类定义,反之父域得不到子域的。和继承关系不同的是,如果子域中有和父域同名的类,子域定义会被忽略而使用父域的定义。
  • 加载到同域(运行时共享库)
    类似集合里的合并关系。被加载swf里的所有类定义被合并到当前域中可以直接使用。和加载到子域相同,和当前域同名的定义也会被忽略。
  • 加载到新域(独立运行的程序或模块)
    swf载入指定域之前,先要检查该域及其父域中是否存在同名类,重复定义一概忽略。如果加载别人写的程序,或者使用旧版本的主程序加载新版本的模块,为避免类名冲突就要加载到新域独立运行以使用自己的类。

  模块加载到同域不是一样可以吗?为何要加载到子域呢?好处就在于,卸载一个加载到子域的模块时,只要确保清除所有到该模块的引用,模块的所有类定义将被垃圾回收(Garbage Collection)。
  有两种方式可以访问 ApplicationDomain :

  • ApplicationDomain.currentDomain
    currentDomain是ApplicationDomain的静态变量,表示当前代码 所 在的域。该变量很奇特,在主程序里指向主域,在加载到子域的模块里则指向该模块所在的子域。虽然 ApplicationDomain 有个 parentDomain 属性,但子域已经自动获得了父域的类定义,所以通过 ApplicationDomain.currentDomain 就可以获取父域定义了——包括主程序和加载到主域的共享库。(注:系统域不可直接访问,主域和所有新域即系统域子域的parentDomain属性为 null)
  • LoaderInfo类的applicationDomain属性
    此方式可以访问任何方式加载的swf的 ApplicationDomain。对于主程序来说,加载到同域的库定义已经存在于 ApplicationDomain.currentDomain ,而模块的类主程序一般用不到。所以这种方式个人不推荐使用。

  ApplicationDomain 的 hasDefinition() 方法判断某定义是否存在,getDefinition() 方法获取指定的定义。下面以一个 例子 来介绍 ApplicationDomain 的具体用法和应用程序的拆分。
  本例 有四个swf,shell.swf是主程序,lib.swf是共享库,login.swf和result.swf分别是“登录”和“结果”模块,所有的视 图元件都在共享库中。实际开发时可能有很多库,比如“位图库”、“音效库”、“模型通用库”等。“通用库”里存放多个模块共用的资源,比如此例中的背景元 素。而各个模块独有的资源还是放在各自的swf中。
  主程序首先将共享库加载到同域,完成后将“登录模块”加载到子域。主程序可以像操作普通的 视觉对象(DisplayObject)一样操作加载的模块:监听事件、调用方法。因为编译器不会识别未定义的类,为使用强类型,建议为主类和模型定义相 应的接口,使用少量的重复代码协助编程。

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 ) ;
    
}
}

  模块“继承”了主程序和共享库的所有类和资源,可以通过 ApplicationDomain.currentDomain.getDefinition() 来获取相应的类。注意获取不存在的类会抛出一个 ReferenceError。

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 ;
}

   登录模块获取库中的界面元素,并在点击按钮后抛出事件。Event类不允许带参数,必须使用继承Event的自定义事件抛出参数。主程序可以把模块的自 定义事件也编译进去(这样就增大了整个程序的文件尺寸),或者让监听模块事件的函数接受一个Objcet参数,以获取其动态属性。

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 () ;
}

  主程序收到事件之后卸载注册模块,加载“结果模块”到子域,并将登录模块传出的”userName”参数传给结果模块。

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 )) ;
}

  注意initUi()方法分别使用了共享库中Libaray类的静态属性BG_NAME和静态方法getResult()。但是直接调用此静态方法会报错,可以先用 resultFunc 变量取出此方法。详细内容请参考 源代码

实例效果演示
下载源文件 (rar 278K)

 

 

这篇文章在杜增强的博客看到,感谢他的分享。看完受益非浅!

分享到:
评论

相关推荐

    详细介绍ApplicationDomain和SecurityDomain

    详细介绍ApplicationDomain和SecurityDomain的原理,想对这方面的知识做深入研究的可以看下

    Adobe Flash Player for IE v11.3.300.265

    Adobe Flash Player(Flash Player官方下载)是一个集成的多媒体播放器,让你在Web上享受更广泛的多媒体体验。Adobe Flash Player官方版可显示和播放多媒体内容,从数以千计的高度...— ApplicationDomain.getQualifie

    网页FLASH播放器代码

    <script language="JavaScript"> var movie,timer,step,total,state=null,delay=100 function init(){//在 Flash 加载完成后初始化相关变量的函数 ...<embed name="movie" src="http://file.flash8.net/film/...

    Flash插件本地下载

    34. type="application/x-shockwave-flash" 35. <%if(SysDownloadFlashFashion==1){%> 36. pluginspage="http://www.adobe.com/go/getflashplayer" 37. <% }else{%> 38. pluginspage="<%=context%>/...

    asp flash 上传 代码 能显示上传进度

    <embed src="upfile/main.swf" quality="high" bgcolor="#ffffff" width="342" height="38" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage=...

    详解flash应用程序域

    读懂flash应用程序域的用法,体会安全沙箱机制。

    FLEX 系统管理器 SystemManager

    SystemManager是Flex应用的主控者,它控制着应用窗口,Application实例,弹出窗口,cursors,并管理着ApplicationDomain中的类。SystemManager是FlashPlayer实例化的第一个类,它存储了主应用窗

    AS3-DomainMemoryManager:Adobe AIRFlash中用于域内存访问的管理器

    AS3-DomainMemoryManager 用于在Adobe AIR / Flash中访问域内存的管理器。 该项目是根据MIT许可... DomainMemory的使用存在一些问题: 只能将一个字节数组同时分配给ApplicationDomain.domainMemory 在ApplicationDo

    flash首页图片.rar

    +button_pos+'&stop_time='+stop_time+'" quality="high" width="'+ pic_width +'" height="'+ swf_height +'" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage=...

    PROGRAMMING ACTIONSCRIPT 3.0

    Using the ApplicationDomain class700 Using the IME class. 704 Example: Detecting system capabilities 709 12 Chapter 24: Printing... 715 Basics of printing..716 Printing a page..718 Flash Player tasks ...

    SWFUpload大文件上传系统

    SWFUpload 是一个小型的、利用 JavaScript/Flash 技术相结合的上传组件。它具有强大的功能在融合了 JavaScript/Flash 技术后,且简易的 HTML/CSS 搭配使您更容易做出属于自己的上传网站。它的强大功能包括有: 可...

    SSCP.ISC2.Systems.Security.Certified.Practitioner.Official.Study.Guide

    Fully updated Study Guide for the SSCP ...Chapter 9 Domain 7: Systems and Application Security Appendix A Answers to Written Labs Appendix B Answers to Review Questions Appendix C Diagnostic Tools

    Spring Recipes A Problem-Solution Approach [英文原版]

    One of the first (if not the ... Frameworks built on top of the Spring framework have emerged supporting application integration, batch processing, Flex and Flash integration, GWT, OSGi, and much more.

    可控制切换显示图片,js代码

    <SCRIPT type=text/javascript>var pic_width=331;...type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');document.write('</object>');</SCRIPT>

    赛尔号发包例子以及逆向过程-易语言

     this._loader.loadBytes(byteArray,new LoaderContext(false,ApplicationDomain.currentDomain));  } 思路清晰,可以干代码,这里使用了易语言 导出文件 可以,再将其导入FFdec看看效果。解压后,开始找与通讯的...

    asp.net知识库

    技术基础 New Folder 多样式星期名字转换 [Design, C#] .NET关于string转换的一个小Bug Regular Expressions 完整的在.net后台执行javascript脚本集合 ASP.NET 中的正则表达式 常用的匹配正则表达式和实例 ...

    ASP网页幻灯片代码另类

    type=""application/x-shockwave-flash"" pluginspage=""http://www.macromedia.com/go/getflashplayer"" />'');" Response.Write "document.write(''</object>'');" Response.Write "</SCRIPT>" rs.close set rs...

    数据库系统概念Database System Concept(英文第6版)文字版

    6.3 The Domain Relational Calculus 245 6.4 Summary 248 Exercises 249 Bibliographical Notes 254 Chapter 7 Database Design and the E-R Model 7.1 Overview of the Design Process 259 7.2 The Entity-...

    WordPress 3 Search Engine Optimization.pdf

    Flash, JavaScript, image links, and other non-readable content 248 Poor site architecture 250 Table of Contents [ viii ] Expert site architecture with manual links and PageRank sculpting 251 Over-...

    spring-framework-reference4.1.4

    Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................

Global site tag (gtag.js) - Google Analytics