用反射在运行时拿到加载进来的swf的Class

 

package {
    import flash.display.MovieClip;
    import flash.display.Loader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.display.DisplayObject;
    import flash.system.ApplicationDomain;
    import flash.utils.getDefinitionByName;
    /*
     *
     * 用反射在运行时拿到加载进来的swf的Class
     */
    public class Main extends MovieClip {
        private var loader : Loader = new Loader();
        private var path : URLRequest = new URLRequest("Test.swf"); //要加载的swf
        public function Main() {
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
            loader.load(path);
        }
        private function loaded(evt : Event) : void{
            trace(evt.target);
            var swf : DisplayObject = evt.target.content as DisplayObject;
            addChild(swf);
            
            var classRef : Object = swf.loaderInfo.applicationDomain.getDefinition("Test"); //获取加载进来的swf的Class引用
            var ff : DisplayObject = new classRef()as DisplayObject; //创建一个新的Test类的实例
            addChild(ff);
            ff.x = 100;
            
        }
    }
}

 

 

 

posted @ 2012-10-17 11:16  playerlife  阅读(225)  评论(0编辑  收藏  举报