flash动态改变注册点

来源:互联网 发布:淘宝子账号没有二维码 编辑:程序博客网 时间:2024/05/22 14:45

这是一个动态改变注册点的类

package {//动态改变注册点类import flash.display.DisplayObject;import flash.geom.Point;//动态设置注册点public class DynamicRegistration {//需更改的注册点位置private var regpoint:Point;//更改注册的显示对象private var target:DisplayObject;private var Height:Number;private var Width:Number;//首先要确定初始状态public function DynamicRegistration(target:DisplayObject,regpoint:Point,iWidth:Number=320,iHeight:Number=240) {Width=iWidth;Height=iHeight;this.target=target;this.regpoint=regpoint;}//设置显示对象的属性public function flush(prop:String,value:Number):void {var mc=this.target;//转换为全局坐标var A:Point=mc.parent.globalToLocal(mc.localToGlobal(regpoint));if (prop=="x"||prop=="y") {mc[prop]=value-regpoint[prop];} else if (prop=="scaleX" || prop=="scaleY") {if (mc[prop]>value) {//放大过程mc.x+=((Width*(value+0.1))-(Width*value))/4;mc.y+=((Height*(value+0.1))-(Height*value))/4;} else {//缩小过程mc.x-=((Width*value)-(Width*(value-0.1)))/4;mc.y-=((Height*value)-(Height*(value-0.1)))/4;}mc[prop]=value;} else {mc[prop]=value;//执行旋转等属性后,再重新计算全局坐标var B:Point=mc.parent.globalToLocal(mc.localToGlobal(regpoint));//把注册点从B点移到A点mc.x+=A.x-B.x;mc.y+=A.y-B.y;}}}}


用法如下:

在舞台上放个mc、btn、btn2

package{import flash.display.*;import flash.events.*;import flash.geom.*;import flash.system.fscommand;public class Test extends MovieClip{private var reg:DynamicRegistration;private var speed:Number=1;public function Test(){reg=new DynamicRegistration(mc,new Point(mc.width/2,mc.height/2),mc.width,mc.height);btn.addEventListener(MouseEvent.CLICK,clickHandler);btn2.addEventListener(MouseEvent.CLICK,lessenHandler);}private function clickHandler(e:MouseEvent):void{//mc.scaleX+=0.2;//mc.scaleY+=0.2;speed+=0.2;            reg.flush("scaleX",speed);             reg.flush("scaleY",speed);}private function lessenHandler(e:MouseEvent):void{speed-=0.2;            reg.flush("scaleX",speed);             reg.flush("scaleY",speed);//mc.scaleX-=0.2;//mc.scaleY-=0.2;}}}


 

 

原创粉丝点击