as3调用外部swf里的类的方法
2013-01-04 13:31
106 查看
as3项目要调用外部swf里的类有3种方法:
1.将外部的swf发布为swc,使用时将swc引用添加到相应的项目中,这应该是最简单的一种引用。不过当项目中的类或组件比较多时就会使项目发布生成的swf文件大小过大;
2.通过资源绑定外部的,然后直接通过类名获取。如:[Embed(source="assets/icon/skin.swf",symbol="Btn_Max")],这种方法也会引起swf文件过大;
3.通过域来来获取外部swf里的绑定类,这种方法可以在需要用时才去加载相应的swf文件然后再获取所需要的类。
下面是根据第三种方法来获取所需要的类:
View Code
1.将外部的swf发布为swc,使用时将swc引用添加到相应的项目中,这应该是最简单的一种引用。不过当项目中的类或组件比较多时就会使项目发布生成的swf文件大小过大;
2.通过资源绑定外部的,然后直接通过类名获取。如:[Embed(source="assets/icon/skin.swf",symbol="Btn_Max")],这种方法也会引起swf文件过大;
3.通过域来来获取外部swf里的绑定类,这种方法可以在需要用时才去加载相应的swf文件然后再获取所需要的类。
下面是根据第三种方法来获取所需要的类:
View Code
package com.tflash.Components.util { import flash.display.DisplayObject; import flash.display.Loader; import flash.display.LoaderInfo; import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IEventDispatcher; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.net.URLRequest; import flash.system.ApplicationDomain; import flash.system.LoaderContext; [Event(name="complete", type="flash.events.Event")] [Event(name="progress",type="flash.events.ProgressEvent")] [Event(name="io_error",type="flash.events.IOErrorEvent")] public class SWFLoader extends EventDispatcher { private var loader:Loader; private var content:DisplayObject; private var loadComplete:Boolean=false; private var url:String; public function SWFLoader(url:String) { this.url=url; } public function Load(url:String=null):void{ if(url!=null){ this.url=url; } loadComplete=false; if(loader==null){ loader=new Loader(); }else{ loader.unloadAndStop(true); if(loader.contentLoaderInfo.hasEventListener(Event.COMPLETE)){ loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoadComplete); } if(loader.contentLoaderInfo.hasEventListener(ProgressEvent.PROGRESS)){ loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError); } if(loader.contentLoaderInfo.hasEventListener(IOErrorEvent.IO_ERROR)){ loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError) } } var context:LoaderContext=new LoaderContext(); context.applicationDomain=new ApplicationDomain(ApplicationDomain.currentDomain); var request:URLRequest=new URLRequest(this.url); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete); loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onLoadProgress); loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onLoadIOError); loader.load(request,context); } private function onLoadProgress(evt:ProgressEvent):void{ this.dispatchEvent(evt); } private function onLoadComplete(evt:Event):void{ evt.currentTarget.removeEventListener(Event.COMPLETE,onLoadComplete); evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError); evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError); content=(evt.currentTarget as LoaderInfo).content; loadComplete=true; this.dispatchEvent(new Event(Event.COMPLETE)); } private function onLoadIOError(evt:IOErrorEvent):void{ evt.currentTarget.removeEventListener(Event.COMPLETE,onLoadComplete); evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError); evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR,onLoadIOError); this.dispatchEvent(evt); } /** * 获取当前ApplicationDomain内的类定义 * * name类名称,必须包含完整的命名空间,如 Grave.Function.SWFLoader * info加载swf的LoadInfo,不指定则从当前域获取 * return获取的类定义,如果不存在返回null */ public function GetClass(name:String):Class{ if(loadComplete&&loader!=null){ if(loader.contentLoaderInfo.applicationDomain.hasDefinition(name)){ return loader.contentLoaderInfo.applicationDomain.getDefinition(name) as Class; }else{ return null; } } return null; } public function GetContent():DisplayObject{ return content; } public function GetLoader():Loader{ return loader; } } }
相关文章推荐
- as3调用外部swf里的类的方法
- flex 打开外部的swf 并调用其方法
- AS3运行时加载外部库,调用swf中的库
- AS3 FLASH,加载外部swf的loading方法
- AS3 从外部SWF中获取资源的方法(ApplicationDomain的使用)
- 如何通过AS3加载外部SWF文件,调用外部文件文档类的方法?
- AS3 调用外部SWF中元件库中的元件 【转】
- 如何通过AS3加载外部SWF文件,调用外部文件文档类的方法?
- 如何通过AS3加载外部SWF文件,调用外部文件文档类的方法?
- Load 外部swf并调用其中的对像元件的方法
- AS3 调用外部SWF中元件库中的元件
- AS3 调用外部SWF中元件库中的元件 【转】
- 通过AS3加载外部SWF文件,调用外部文件文档类的方法
- Vue中使用Froala Editor时,外部方法调用html.insert()插入内容到光标最后停留位置功能实现
- 接口的案例3(外部类对接口中方法的调用)
- wpf调用外部线程操作ui方法
- vc调用外部程序的方法
- 调用外部js方法
- IOS开发笔记之十二——Xcode下类中供外部调用的方法添加注释说明技巧
- 用接口实现通过改外部文件实现判断调用接口内的方法