2006/09/19 | AS3链接外部swf里的mc
类别(Flex&AS3) | 评论(1) | 阅读(667) | 发表于 16:02
借用了云开前辈的这个swfLib.as,在上面稍微改了下,终于可以在AS3中实现加载外部swf的图片,以后我们就可以直接把图片资源都往这里面仍,只用写个link_id就行了,这样就做到运行时main.swf调用图片资源swf里的mc了,真是个好东西。

//swfLib.as
package
{
  import flash.display.Sprite;
  import flash.display.MovieClip;
  import flash.utils.getDefinitionByName;
  public class SWFLib extends Sprite
  {
    public function SWFLib(){
    }
    public function getMC(p_linkId:String){
      try{
        var result:Class=getDefinitionByName(p_linkId) as Class;
        return result;
      }catch(e:Error){
        trace("error~~~~~~~~~~~~~~~~~");
        return null;
      }
    }
  }
  
}
//Main.as
package{
  import flash.display.Sprite;
  import flash.display.Loader;
  import flash.net.URLRequest;
  import flash.events.*;
  import flash.display.stage;
  public class Main extends Sprite{
    private var myLoader:Loader;
    private var swfLib:String = "swfLib.swf";
    private var rec;
    private var count:Number = 0;
    public function Main(){
myLoader = new Loader();
      var myURLRequest:URLRequest = new URLRequest(swfLib);
      myLoader.load(myURLRequest);
      addChild(myLoader);
      myLoader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
      stage.addEventListener(MouseEvent.CLICK,clickHandler);
      
    }
    private function initHandler(e:Event):void{
       var recClass:Class = e.target.content.getMC("Rec_mc");
       rec_mc=new recClass();
       rec_mc.x = 100;
       rec_mc.y = 100;
       addChild(rec_mc);
    }
    
  }
}
0

评论Comments

日志分类
首页[116]
Flash&AS2[56]
FMS[3]
Flex&AS3[5]
Asp[5]
My Works[13]
My Life[34]