Actionscript 3: Rotating an object from an arbitrary point.
I’ve run into this problem a few times I want to rotate and object by an arbitrary point. First off actionscript doesn’t let you change you point from which you rotate when you change the rotation attribute of an object. By default when you rotate using the rotation attribute flash rotates the objects at it’s index point (0, 0). Simple solution move the object so it’s center point is at the index point of it’s container move clip then rotate it, then move it back it’s original position. To make things easier you can of course wrap this up in a function inside of an object or use this snippet.
View CodeATIONSCRIPT | |
function rotate(item:DisplayObject, angle:Number, point:Point) {
var matrix:Matrix = item.transform.matrix.clone();
matrix.translate(-point.x, -point.y);
matrix.rotate (angle*(Math.PI/180));
matrix.translate(point.x, point.y);
item.transform.matrix = matrix;
} | |
Simple enough to use just pass the function the moveclip you want to rotate the angle just like you would with the rotation attribute and the point from the index point that you want to rotate your clip.
rotate(thing, 90, new Point(50, 50));