AS3 Rotate an object around its center point

后端 未结 2 479
小蘑菇
小蘑菇 2020-12-19 02:11

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

        switch (event.keyCode)
        {
                   


        
2条回答
  •  忘掉有多难
    2020-12-19 02:38

    The following will rotate around center :

    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);
    }
    

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

提交回复
热议问题