AS3动态改变注册点

package as3.zcd{
import flash.display.MovieClip;
import flash.geom.Point;
import flash.display.DisplayObject;
public class Zcd{
  private var mc:MovieClip;
  private var p:Point;
  public function Zcd(m:MovieClip,point:Point){
   mc = m;
   p = point;
   RegPoint();
  }
  private function RegPoint():void {
       var tmp_point:Point=mc.parent.globalToLocal(mc.localToGlobal(p));
       var len:int=mc.numChildren;
       while (len--) {
               var tmp_obj:DisplayObject=mc.getChildAt(len);
               tmp_obj.x-=p.x;
               tmp_obj.y-=p.y;
       }
       mc.x=tmp_point.x;
       mc.y=tmp_point.y;
  }
}
}

创建一个mc放入主场景 命名实例名为 a_mc 另外在放两个按钮 命名实例名分别为 start_btn,stop_btn

然后在第一帧 写下如下代码

import as3.zcd.Zcd;
var zhucedian:Zcd;
var t:Timer = new Timer(50);
t.addEventListener(TimerEvent.TIMER,ontimer);
this.start_btn.addEventListener(MouseEvent.CLICK,onstart);
function onstart(event:MouseEvent):void{
t.start();
}
this.stop_btn.addEventListener(MouseEvent.CLICK,onstop);
function onstop(event:MouseEvent):void{
t.stop();
}
function ontimer(event:TimerEvent):void{
this.a_mc.rotation+=5;
}
this.a_mc.addEventListener(MouseEvent.CLICK,onaclick);
function onaclick(event:MouseEvent):void{
zhucedian = new Zcd(this.a_mc,new Point(this.a_mc.mouseX,this.a_mc.mouseY));
}

开始测试 点击那个场景中的mc一次 然后点击开始按钮看mc的旋转点 已经改变了吧 呵呵


你可能感兴趣的:(AS3动态改变注册点)