flash 和flex中的所有可视对象的注册点全部都是默认在(0,0)点,也就是左上角.在flash内编辑状态下 可以调整注册点位置但要是程序生成的就没有办法了 因为还没发现有注册点属性.只操作位置 x,y时注册点在什么位置并不重要 但要是操作旋转rotation 缩放scaleX,scaleY 注册点在上面总感觉别扭,习惯上的位置应该是几何中心.
调整方法(一):双层容器嵌套;
这中方法就是 用两层容器包裹起对象 内层容器的几何中心放在外层的注册点位置 也就是说内层的xy位width/2,height/2.这样一来就技巧的实现了对外层的操作转化成了内层几何中心的操作.
调整方法(二)动态调整坐标
其实注册点的不同,产生的效果就是操作或的位置不同.那么根据不同的操作和注册点的偏差,进行调整也能实现对注册点的改变,而且这种方式更直接和方便.自己看类的结构就明白了.
package com.makyoo.geometry
{
//www.makyoo.cn QQ:84407979
//动态改变注册点类
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注册点————我能找到的,想到的
2009年04月07日 星期二 18:41
动态改变MC注册点(一)--原理
转自 蓝色理想
无论是采用哪种办法来动态改变MC的注册点,其实质都是把MC变化之前的位置和变化之后的位置记下,然后算出某个点在这个过程中向什么地方变化了,然后把 这个点重置回原处,并没有真正的象我们制作MC时那样把那个小加号(注册点)改了位置,也就是说,所谓的动态改变MC注册点是一种修正方法,在视觉上使人 感觉MC的注册点改变了。
为什么要谈到MC变化之前,变化之后呢?因为没有变化,我们是看不出来注册点到底在哪,当MC缩放时,我们可以观察到注册点,最易观察到注册点位置的变化状态是旋转,下面就以旋转为例,说说MC注册点调整的原理。
具体解释见http://yjbzl.blog.china.com/200903/4520670.html
实现代码如下,创建MC,起名fang,当点击MC时,它会以点击点为中心进行旋转
fang.addEventListener(MouseEvent.CLICK,rota);
var xx:Number;
var yy:Number;
var newxx:Number;
var newyy:Number;
function rota(e:MouseEvent) {
var mymc:MovieClip=e.currentTarget as MovieClip;
var mypt:Point=new Point(mymc.mouseX,mymc.mouseY);
xx=mymc.localToGlobal(mypt).x;
yy=mymc.localToGlobal(mypt).y;
mymc.rotation+=10;
//旋转之后,刚才的点mymc.mouseX,y,相对于舞台的全局坐标和旋转之前不一样了,有一个差值,我们就用这个差值把MC的位置调整一下。
newxx=mymc.localToGlobal(mypt).x;
newyy=mymc.localToGlobal(mypt).y;
fang.x=fang.x-(newxx-xx);
fang.y=fang.y-(newyy-yy);
}
在网上查到的一些改变MC注册点的方法(二)利用DynamicRegistration 类一、类文件:
package {//动态设置注册点
import flash.display.DisplayObject;
import flash.geom.Point;
public class DynamicRegistration {
//需更改的注册点位置
private var regpoint:Point;
//更改注册点的显示对象
private var target: DisplayObject;
function DynamicRegistration(target: DisplayObject,regpoint:Point) {
this.target=target;
this.regpoint=regpoint;
}
//设置显示对象的属性
public function flush(prop:String,value:Number):void {
var mc=this.target;
//转换为全局坐标
var A:Point=mc.localToGlobal(regpoint);
mc[prop]+=value;
//执行旋转等属性后,再重新计算全局坐标
var B:Point=mc.localToGlobal(regpoint);
//把注册点从B点移到A点
mc.x-=B.x-A.x;
mc.y-=B.y-A.y;
}
}
}
二、这个类的使用方法:
import DynamicRegistration;
mymc.addEventListener(MouseEvent.CLICK,myfun);
function myfun(e:MouseEvent) {
var mypt:Point=new Point(e.currentTarget.mouseX,e.currentTarget.mouseY);
var myname: DynamicRegistration=new DynamicRegistration(mymc,mypt);
myname.flush("rotation",10);
}
改变MC注册点的方法(三)rotateAroundInternalPoint这是一个静态方法,开始没搞清,以为是一个属于某实例的方法,然后直接写成MC.rotateAroundInternalPoint(参数,,,,,,),结果报错,仔细看了下说明,public static function,静态方法是属于类的,不属于实例。
这个算是最简单的,不用管原理,直接给参数用就行了。
//代码如下
import fl.motion.Motion;
var m:Matrix=new Matrix ;
fang.addEventListener(MouseEvent.CLICK,rota);
function rota(e:MouseEvent) {
fl.motion.MatrixTransformer.rotateAroundInternalPoint(m,e.currentTarget.mouseX,e.currentTarget.mouseY,10);
fang.transform.matrix=m;
}
不过关于这个Matrix,一点也不明白它。
结束语:同一个效果,可以有很多方法来实现,弄清原理最好,实在弄不清了,用现成的类也不错。
动态修改MC的注册点
引用地址:
文章地址:http://www.darronschall.com/weblog/archives/000054.cfm
下载地址:http://www.darronschall.com/downloads/DynamicRegistration.zip
演示:
Flash Player文件
使用方法:
import com.darronschall.DynamicRegistration;
// Assume there is an instance named square_mc on the stage that
// has the correct linkage as described above
var square_mc:DynamicRegistration;
// The square_mc has an original registration at 0,0 so
// let's change that to 10, 60 at runtime.
square_mc.setRegistration(10, 10);
// Now whenever we access a property of the square_mc that deals
// with the registration point, use a "2" after the property name...
// These are the available properties:
square_mc._x2 = 4;
square_mc._y2 = 7;
square_mc._rotation2 = 40;
square_mc._xscale2 = 140;
square_mc._yscale2 = 80;
// square_mc._xmouse2 is readonly
// square_mc._ymouse2 is readonly
核心代码:
// special thanks to Robert Penner (www.robertpenner.com) for providing the
// original code for this in ActionScript 1 on the FlashCoders mailing list
dynamic class com.darronschall.DynamicRegistration {
private function DynamicRegistration() {
}
public static function initialize(target_mc):Void {
var p = _global.com.darronschall.DynamicRegistration.prototype;
target_mc.xreg = 0;
target_mc.yreg = 0;
target_mc.setRegistration = p.setRegistration;
target_mc.setPropRel = p.setPropRel;
with (target_mc) {
addProperty("_x2", p.get_x2, p.set_x2);
addProperty("_y2", p.get_y2, p.set_y2);
addProperty("_xscale2", p.get_xscale2, p.set_xscale2);
addProperty("_yscale2", p.get_yscale2, p.set_yscale2);
addProperty("_rotation2", p.get_rotation2, p.set_rotation2);
addProperty("_xmouse2", p.get_xmouse2, null);
addProperty("_ymouse2", p.get_ymouse2, null);
}
}
private function setRegistration(x:Number, y:Number):Void {
this.xreg = x;
this.yreg = y;
}
private function get_x2():Number {
var a = {x:this.xreg, y:this.yreg};
this.localToGlobal(a);
this._parent.globalToLocal(a);
return a.x;
}
private function set_x2(value:Number):Void {
var a = {x:this. xreg, y:this. yreg};;
this.localToGlobal(a);
this._parent.globalToLocal(a);
this._x += value - a.x;
}
private function get_y2():Number {
var a = {x:this.xreg, y:this.yreg};
this.localToGlobal(a);
this._parent.globalToLocal(a);
return a.y;
}
private function set_y2(value:Number):Void {
var a = {x:this.xreg, y:this.yreg};
this.localToGlobal(a);
this._parent.globalToLocal(a);
this._y += value - a.y;
}
private function set_xscale2(value:Number):Void {
this.setPropRel("_xscale", value);
}
private function get_xscale2():Number {
return this._xscale;
}
private function set_yscale2(value:Number):Void {
this.setPropRel("_yscale", value);
}
private function get_yscale2():Number {
return this._yscale;
}
private function set_rotation2(value:Number):Void {
this.setPropRel("_rotation", value);
}
private function get_rotation2():Number {
return this._rotation;
}
private function get_xmouse2():Number {
return this._xmouse - this.xreg;
}
private function get_ymouse2():Number {
return this._ymouse - this.yreg;
}
private function setPropRel(property:String, amount:Number):Void {
var a = {x:this.xreg, y:this.yreg};
this.localToGlobal (a);
this._parent.globalToLocal (a);
this[property] = amount;
var b = {x:this.xreg, y:this.yreg};
this.localToGlobal (b);
this._parent.globalToLocal (b);
this._x -= b.x - a.x;
this._y -= b.y - a.y;
}
}
相关日志