参考文章: http://www.blueidea.com/tech/multimedia/2008/5842_2.asp
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/djy1135/archive/2009/11/13/4807925.aspx
一、目的
bb.swf的元件库中有一个元件,在aa.swf中调用这个元件,显示到舞台上
二、步骤
bb.swf
1.新建bb.fla,在上面画一个圆
2.选择这个圆,将其转换为元件,并选择”Export for ActionScript”,类名取为”Yuan”
3.在舞台上删除这个元件(删除后,只在元件库中有该元件)
4.Ctrl+Enter 运行bb.fla , 生成bb.swf
aa.swf
1.新建aa.fla
2.在时间轴上加如下代码
var cl:ClassLoader = new ClassLoader(“bb.swf”); //加载bb.swf文件
cl.addEventListener(Event.COMPLETE, cm_fun);
function cm_fun(e:Event){
var Yuan:Class = cl.getClass(“Yuan”) as Class;
var yuan:DisplayObject = new Yuan();
yuan.x = 50;
yuan.y = 100;
yuan.alpha = .5;
addChild(yuan);
}
三、类加载文件
package {
import flash.display.Loader;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.EventDispatcher;
import flash.system.LoaderContext;
public class ClassLoader extends EventDispatcher{
public var url:String;
public var loader:Loader;
//构造函数
public function ClassLoader(obj:Object = null,lc:LoaderContext = null) {
if(obj != null){
if(obj is ByteArray){
loadBytes(obj as ByteArray,lc);
}else if(obj is String){
load(obj as String,lc);
}else{
throw new Error(“参数错误,构造函数第一参数只接受ByteArray或String”);
}
}
}
//加载
public function load(_url:String,lc:LoaderContext = null):void{
url = _url;
loader = new Loader;
loader.load(new URLRequest(url),lc);
addEvent();
}
//加载字节
public function loadBytes(bytes:ByteArray,lc:LoaderContext = null):void{
loader = new Loader;
loader.loadBytes(bytes,lc);
addEvent();
}
//开始侦听
private function addEvent():void{
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressFun);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeFun);
}
//结束侦听
private function delEvent():void{
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,progressFun);
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeFun);
}
//加载成功,发布成功事件
private function completeFun(e:Event):void {
delEvent();
dispatchEvent(e);
}
//加载过程
private function progressFun(e:ProgressEvent):void{
dispatchEvent(e);
}
//获取定义
public function getClass(className:String):Object {
return loader.contentLoaderInfo.applicationDomain.getDefinition(className);
}
//是否含有该定义
public function hasClass(className:String):Boolean {
return loader.contentLoaderInfo.applicationDomain.hasDefinition(className);
}
//清除
public function clear():void{
loader.unload();
loader = null;
}
}
}