【转】引用 flash As3 使用外部swf文件的元件庫中的導出類

flash As3 使用外部swf文件的元件庫中的導出類

1.我們製作一個library.swf在場景中央放一個test_mc,在元件庫中導出link:TestMC, 我們將在main.swf中使用test_mc及庫元件TestMC

2.製作main.swf:它的文檔類main.as如下:

 1 package {
 2 import flash.display.Sprite;
 3 import flash.events.Event;
 4 
 5 
 6 public class Main extends Sprite {
 7   
 8    private var swfload:SWFLoader;
 9 
10    public function Main():void {
11     //加载外部SWF
12     swfload = new SWFLoader;
13     swfload.addEventListener(Event.COMPLETE,onComplete);
14     swfload.Load("library.swf");
15    }
16 
17    //加载完成
18    function onComplete(e:Event):void {
19    
20     //这里是关键
21     /*swfload.loadinfo 注释:LoadInfo对象*/
22     /*TestMc            注释:读取应用程序域中的连接类名*/
23     /*调用SWFLoader中的GetClass方法 类已封装,传参数调用即可*/
24     var Tempclass:Class = swfload.GetClass("TestMc");
25 
26     if (!Tempclass) {
27      return;
28     }
29     //实例化类库中的类
30     var mc:* = new Tempclass();
31     mc.x = 100;
32     mc.y = 100;
33 
34     //还能调用该类中的动态文本
35     mc.txt.text = "还能调用我里面的动态文本哩!";
36     this.addChild(mc);
37     this.addChild(swfload.this_mc);
38     //++++++++++++++++++++++++++++++++++++++++++++++++++圆满完成(END)~~~ oliwen
39    }
40 }
41 }

 

3,加載library.swf的代碼as文件:SWFLoader.as如下:

 1 /*
 2 * SWF加载
 3 */
 4 package {
 5 import flash.display.*;
 6 import flash.display.Loader;
 7 import flash.display.LoaderInfo;
 8 import flash.events.Event;
 9 import flash.events.EventDispatcher;
10 import flash.net.URLRequest;
11 import flash.system.ApplicationDomain;
12 import flash.system.LoaderContext;
13 /**
14 * SWF加载器
15 */
16 public class SWFLoader extends EventDispatcher {
17    private var m_loadinfo:LoaderInfo;
18    public var this_mc:MovieClip = new MovieClip();
19    //*******************
20    //constructor
21    //*******************
22    public function SWFLoader() {
23    }
24   
25    //load swf
26    public function Load( {
27     var loader:Loader = new Loader();
28     var request:URLRequest = new URLRequest(url);
29     var context:LoaderContext = new LoaderContext();
30    
31     //loaded into the sub-domain
32     context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
33     loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
34    
35     loader.load(request, context);
36    }
37   
38    /**
39    * 获取当前ApplicationDomain内的类定义
40    * 
41    * name类名称,必须包含完整的命名空间,如 Grave.Function.SWFLoader
42    * info加载swf的LoadInfo,不指定则从当前域获取
43    * return获取的类定义,如果不存在返回null
44    */
45    
46    public function GetClass(name:String):Class {
47     if (m_loadinfo.applicationDomain.hasDefinition(name)) {
48      return m_loadinfo.applicationDomain.getDefinition(name) as Class;
49     }
50     return null;
51    }
52   
53    //load complete event
54    private function onComplete(e:Event):void {
55     e.currentTarget.removeEventListener(Event.COMPLETE,onComplete);
56     this_mc = e.currentTarget.content;
57     m_loadinfo = e.currentTarget as LoaderInfo;
58     dispatchEvent(new Event(Event.COMPLETE));
59    }
60 }
61 }

 

4.你可以測試使用了。看到效果了嗎?

posted @ 2012-10-28 14:22  雅尸兰黛?窃笑  阅读(167)  评论(0)    收藏  举报