我正在尝试实现转换方法以在运行时旋转和调整动画片段的大小,并且在重新计算旋转以跟随鼠标时出现问题。
我在广场的右下角有一个方形的动画片段和一个旋转物体(一个圆圈),我正在听鼠标事件和鼠标移动我这样做:
_rotation = -(Math.atan2((event.stageX - this.x ), (event.stageY - this.y ))) * 180/Math.PI;
this.rotation = rotation + 45;//this is because my rotator object is on right,bottom corner
只要我不修改对象的宽度或高度,这就行得很完美,但是如果我修改那么对象的旋转会有一个小的“跳跃”,这是我无法避免的。
我知道这是由于event.stageX和even.stageY不同,因为旋转器对象(带有鼠标侦听器的对象)在调整大小事件后已经移动但是没有线索如何避免“跳转”。
请原谅我的英文不好
答案 0 :(得分:0)
您需要围绕其中心旋转对象。
/**
* Rotates the object based on its center
* Parameters: @obj => the object to rotate
* @ rotation => angle to rotate
* */
public function RotateAroundCenter(obj:Object, rotation:Number):void
{
var bound:Rectangle = new Rectangle();
// get the bounded rectangle of objects
bound = obj.getRect(this);
// calculate mid poits
var midx1:Number = bound.x + bound.width/2;
var midy1:Number = bound.y + bound.height/2;
// assign the rotation
obj.rotation = rotation;
// assign the previous mid point as (x,y)
obj.x = midx1;
obj.y = midy1;
// get the new bounded rectangle of objects
bound = obj.getRect(this);
// calculate new mid points
var midx2:Number = bound.x + bound.width/2;
var midy2:Number = bound.y + bound.height/2;
// calculate differnece between the current mid and (x,y) and subtract
//it to position the object in the previous bound.
var diff:Number = midx2 - obj.x;
obj.x -= diff;
diff = midy2 - obj.y;
obj.y -= diff;
}
用法:
_rotation = -(Math.atan2((event.stageX - this.x ), (event.stageY - this.y ))) * 180/Math.PI;
RotateAroundCenter(this, rotation + 45);
选中此link