这是一个动态改变注册点的类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、btn2package{
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;
}
}
}
//动态改变注册点类
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、btn2package{
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;
}
}
}