AS3旋转围绕其中心点的对象中心点、对象

2023-09-09 21:34:12 作者:别摆脸子!

欲绕其中心而非左上角旋转此物体。 在code是这样的:

I want this object to rotate around its center rather than the top left corner. The code looks like this:

        switch (event.keyCode)
        {
            case 37:
            car.rotation = -90;
               car.x -= 5;
               break;

所以,当我preSS左键,车向左转,但因为它是现在它跳了一下,因为它围绕旋转的右上角。

So when i press the left key, the car turns left but as it is now it jumps up a bit because its rotating around the top corner.

感谢

推荐答案

下面将围绕中心旋转:

public function rotateAroundCenter(object:DisplayObject, angleDegrees:Number):void {
    if (object.rotation == angleDegrees) {
        return;
    }

    var matrix:Matrix = object.transform.matrix;
    var rect:Rectangle = object.getBounds(object.parent);

    matrix.translate(-(rect.left + (rect.width / 2)), -(rect.top + (rect.height / 2)));
    matrix.rotate((angleDegrees / 180) * Math.PI);
    matrix.translate(rect.left + (rect.width / 2), rect.top + (rect.height / 2));
    object.transform.matrix = matrix;

    object.rotation = Math.round(object.rotation);
}

有翻译对象的中心到0,0然后转动,然后将其翻译回

It translates the center of the object to 0,0 then rotate it and then translate it back.