I have a question concerning the use of the matrix object
The following code works well to rotate the sprite from its center if its x and y properties don’t change.
However, if I drag and drop the clip somewhere else after having rotated it, and then add a new rotation, it keeps rotating from the previous rotation point. Even if I add point =new Point(myspr.x+myspr.width/2, myspr.y+myspr.height/2); in a function that is called at the end of the drag and drop, the rotation point remains the same.
So, do you have any idea how to update the position of the matrix rotation point if the x and the y of the clip change on the scene after a rotation? (I hope my question is clear enough)
Thanks
var myspr=new Sprite();
myspr.x=100;
myspr.y=100;
addChild (myspr);
var point:Point=new Point(myspr.x+myspr.width/2, myspr.y+myspr.height/2);
rotateAroundCenter(myspr,45);
function rotateAroundCenter (ob:*, angleDegrees) {
var m:Matrix=ob.transform.matrix;
m.tx -= point.x;
m.ty -= point.y;
m.rotate (angleDegrees*(Math.PI/180));
m.tx += point.x;
m.ty += point.y;
ob.transform.matrix=m;
}
you would just put the new point inside the function.
function rotateAroundCenter (ob:*, angleDegrees) {
var point:Point = new Point(ob.x + ob.width/2,ob.y + ob.height/2);
var m:Matrix=ob.transform.matrix;
m.tx -= point.x;
m.ty -= point.y;
m.rotate (angleDegrees*(Math.PI/180));
m.tx += point.x;
m.ty += point.y;
ob.transform.matrix=m;
}
but this will not work more then once. did you test if you rotate an object more then once? (myspr.x+myspr.width/2, myspr.y+myspr.height/2) is the center of a non-rotated object, but once you rotate it, thats no longer the center of the object. so it will begin to rotate out of control and off the screen if you keep rotating it that way.
if you want to do it similar to that way:
import fl.motion.MatrixTransformer;
var myspr=new Sprite();
myspr.x=100;
myspr.y=100;
myspr.centerW = myspr.width/2;
myspr.centerH = myspr.height/2;
addChild (myspr);
rotateAroundCenter(myspr,45);
function rotateAroundCenter (ob:*, angleDegrees) {
var mat:Matrix = ob.transform.matrix
MatrixTransformer.rotateAroundInternalPoint(mat,ob.centerW,ob.centerH,angleDegrees);
ob.transform.matrix = mat;
}
you’d just need to make custom variables of the objects width and height before you rotate it so those aren’t effected by the rotation