//0. translate the center of the DisplayObject to 0,0 (or an arbitrary offset)
//1. then rotate it
//2. and then translate it back.
//using Matrix:
public function centeredRotation(degrees:Number):void {
if (this.rotation == degrees) { return; }
var matrix:Matrix = this.transform.matrix;
var rect:Rectangle = this.getBounds(this.parent);
matrix.translate(-(rect.left + (rect.width*0.5)), -(rect.top + (rect.height*0.5)));
matrix.rotate((degrees / 180) * Math.PI);
matrix.translate(rect.left + (rect.width*0.5), rect.top + (rect.height*0.5));
this.transform.matrix = matrix;
this.rotation = Math.round(this.rotation);
}
//without Matrix
private function centeredRotation(degrees:Number):void{
if (degrees == this.rotation){return;}
var bounds:Rectangle = this.getBounds(this.parent);
var center:Point = new Point(bounds.x + bounds.width*0.5, bounds.y + bounds.height*0.5);
this.rotation = degrees;
bounds = this.getBounds(this.parent);
var newCenter:Point = new Point(bounds.x + bounds.width*0.5, bounds.y + bounds.height*0.5);
this.x += center.x - newCenter.x;
this.y += center.y - newCenter.y;
}
Comments
0 B
|👍
/👎
0 B
|0 👍
/0 👎
0 B
|👍
/👎
0 B
|👍
/👎