AS3 调用外部SWF中元件库中的元件 【转】
參考文章: 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;
?????????? }
???????? }
??????? }
轉載于:https://www.cnblogs.com/chenhaib/archive/2011/12/19/2293357.html
總結
以上是生活随笔為你收集整理的AS3 调用外部SWF中元件库中的元件 【转】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 交行优逸白金卡申请多久能下来?极速批卡大
- 下一篇: 中信小白two联名卡额度一般是多少?额度