as3项目要调用外部swf里的类有3种方法:

  1.将外部的swf发布为swc,使用时将swc引用添加到相应的项目中,这应该是最简单的一种引用。不过当项目中的类或组件比较多时就会使项目发布生成的swf文件大小过大;

  2.通过资源绑定外部的,然后直接通过类名获取。如:[Embed(source="assets/icon/skin.swf",symbol="Btn_Max")],这种方法也会引起swf文件过大;

  3.通过域来来获取外部swf里的绑定类,这种方法可以在需要用时才去加载相应的swf文件然后再获取所需要的类。

  下面是根据第三种方法来获取所需要的类:

package com.mobiano.flipbook.pageeditor
{
import com.flasht.tui.display.TArrow;
import com.mobiano.flipbook.config.FlipBookConfig;
import com.tflash.Components.util.SWFLoader; import flash.display.DisplayObject;
import flash.errors.IOError;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.utils.Dictionary; public class PlugInManager
{
public static var allExternalClass:Object={};
//public var loadingQueue:Object={};
//public var swfLoader:SWFLoader;
public var loadingQueue:Dictionary=new Dictionary();
private static var canInstace:Boolean=false;
private static var instace:PlugInManager; private var filePrefix:String="./files/pageConfig/";
private var fileSuffix:String=".swf";
public function PlugInManager()
{
if(!canInstace){
throw new Error("Can't new PlugInManager");
}
} public static function getInstace():PlugInManager{
if(instace==null){
canInstace=true;
instace=new PlugInManager();
canInstace=false;
} return instace;
} public function getComponent(target:TAnnoPlugIn,cpName:String,extClassName:String):void{
if(cpName==null||cpName.length<1||extClassName==null||extClassName.length<1)return ;
if(allExternalClass.hasOwnProperty()){
//return allExternalClass[cpName];
var swfLoader:SWFLoader=allExternalClass[cpName];
var cl:Class=swfLoader.GetClass(extClassName);
if(cl!=null){
var extObj:IPlugInInterface=createSWFClass(cl);
if(extObj!=null){
target.extObj=extObj;
}
}
}else{
load(target,cpName,extClassName);
}
//return null;
} public function getSwfUrl(cpName):String{
if(cpName!=null){
return filePrefix+cpName+fileSuffix;
}
return null;
} protected function getURLFrom(url:String):String{
return com.mobiano.flipbook.config.FlipBookConfig.getURLForm(url);
} private function load(target:TAnnoPlugIn,cpName:String,extClName:String):void{
var swfUrl:String=getSwfUrl(cpName);
if(swfUrl==null||swfUrl.length<1)return;
swfUrl=getURLFrom(swfUrl); var swfLoader:SWFLoader=new SWFLoader(swfUrl);
swfLoader.addEventListener(Event.COMPLETE,onComplete);
swfLoader.addEventListener(ProgressEvent.PROGRESS,onProgress);
swfLoader.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
var obj:Object={target:target,compontName:cpName,extClassName:extClName}; //loadingQueue[cpName]=obj;
loadingQueue[swfLoader]=obj;
swfLoader.Load(); } private function onComplete(evt:Event):void{
trace(evt.currentTarget);
if(evt.currentTarget is SWFLoader){
var loader:SWFLoader=evt.currentTarget as SWFLoader;
if(loader in loadingQueue){
var obj:Object=loadingQueue[loader];
if(obj["target"]&&obj["compontName"]&&obj["extClassName"]){
var cpName:String=obj["compontName"];
var extClassName:String=obj["extClassName"]; allExternalClass[cpName]=loader; var cl:Class=loader.GetClass(extClassName);
var target:TAnnoPlugIn=obj["target"];
if(cl!=null){
//allExternalClass[cpName]=cl;
var extObj:IPlugInInterface=createSWFClass(cl);
if(extObj!=null){
target.extObj=extObj;
}
}
}
//loader.GetClass(
//var target:TAnnoPlugIn=loadingQueue[loader];
}
}
} private function createSWFClass(cl:Class):IPlugInInterface{
var extObj:IPlugInInterface;
try{
if(cl!=null){
extObj=new cl();
} }catch(e:Error){
return null;
}
return extObj;
} private function onProgress(evt:ProgressEvent):void{ } private function onIOError(evt:IOError):void{
throw new Error("Load swf error:"+evt);
} }
}
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里的类的方法的更多相关文章

  1. AS3 从外部SWF中获取资源的方法(ApplicationDomain的使用)

    package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; ...

  2. 如何通过AS3加载外部SWF文件,调用外部文件文档类的方法?

    一个Flash中通过AS3代码的Loader对象加载另一个SWF文件,并访问其中的文档类中的方法. 简单示例: 主文件:Main.fla, Main.as 被调用的文件:called.swf, Cal ...

  3. Java 扫描实现 Ioc 动态注入,过滤器根据访问url调用自定义注解标记的类及其方法

    扫描实现 Ioc 动态注入 参考: http://www.private-blog.com/2017/11/16/java-%e6%89%ab%e6%8f%8f%e5%ae%9e%e7%8e%b0-i ...

  4. 【python 3.6】调用另一个文件的类的方法

    文件1:test12.py 文件2:test13.py 文件1 如下: #!/usr/bin/python # -*- coding: utf-8 -*- ''' ''' class abcd(obj ...

  5. as3调用外部应用程序 as调用外部exe文件as3调用bat文件 未测试

    private function callTest(event: Event): void{callExe("d:/a.exe");callBat("d:/a.bat&q ...

  6. 执行引入外部 jar 包的类的方法

    liunx 系统中,命令行中语法:(.后面是冒号:) java -cp .:third.jar MyClass windows 系统中命令行的语法:(.后面是分号;) java -cp .;third ...

  7. ant中调用外部ant任务的两种方法

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  8. C# 调用外部dll(转)

    C# 调用外部dll   一.      DLL与应用程序 动态链接库(也称为DLL,即为"Dynamic Link Library"的缩写)是Microsoft Windows最 ...

  9. C#调用外部DLL介绍及使用详解

    一.      DLL与应用程序 动态链接库(也称为DLL,即为“Dynamic Link Library”的缩写)是Microsoft Windows最重要的组成要素之一,打开Windows系统文件 ...

随机推荐

  1. Oracle tnsname.ora 链接问题

    oracle数据库需要配置tns链接 这里我发现了一个问题: 在D:\Oracle\product\10.1.0\Client_3\NETWORK\ADMIN 目录中配置链接字符串的时候要特别注意: ...

  2. Samba 安全漏洞

    漏洞名称: Samba 安全漏洞 CNNVD编号: CNNVD-201403-239 发布时间: 2014-03-17 更新时间: 2014-03-17 危害等级: 中危   漏洞类型: 信任管理 威 ...

  3. Web请求响应简单整理

      简单对Web请求响应如何处理进行的整理,难免有理解不到位,理解有偏差的地方,如有理解有误的地方,希望大牛批评指正. 1.Web开发的定义首先看看微软对Web开发的定义:Web开发是一个指代网页或网 ...

  4. android利用WebView实现浏览器的封装

    android提供了封装浏览器的接口,可以让开发者利用自己的view显示网页内容.今天又实现研究了一下,利用WebView显示浏览器内容,还可以利用 WebViewClient显示自己需要的内容. 参 ...

  5. 序列化类型为XX的对象时检测到循环引用

    /// 产品列表展示 /// </summary> /// <returns></returns> ) { //获得所有组别 Galasys_IBLL.IT_BIZ ...

  6. 静态Web开发 JQuery

    伍章 JQuery 1节介绍JQuery和顶级对象 <<锋利的JQuery>>JQuery官网: http://jquery.com (下载jquery工具)JQuery在线A ...

  7. Memcache缓存与Mongodb数据库的优势和应用

    先说说自己对 Memcache和Mongodb的一些看法,主要是抛砖引玉了,希望看到大家的意见和补充. Memcache Memcache的优势我觉得总结下来主要体现在: 1) 分布式.可以由10台拥 ...

  8. Mobile testing基础之Native、Web、Hybrid、activity、webview

    应用一词指的是app,即application.原生应用指的是能直接运行于当前操作系统的应用程序:web应用指需要在浏览器中运行的网页应用,由于界面体验.功能上都更加强大,可媲美原生应用,故称web应 ...

  9. Java8 Stream API

    Stream是Java8中,操作集合的一个重要特性. 从iteration到Stream操作 当你操作一个集合的时候,你通常的做法是迭代每一个元素,然后处理你想要的事情.举个例子: String co ...

  10. Zabbix探索:Discovery任务、进程以及占用率

    刚刚又报错了,如下所示: Zabbix discoverer processes more than 75% busy 原因是,我配置了一个自动发现的任务.而每个自动发现的任务都会在一定时间内占用一个 ...