AS3 Rotate an object around its center point

后端 未结 2 447
小蘑菇
小蘑菇 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.

    0 讨论(0)
  • 2020-12-19 02:55

    The easiest way to accomplish this is to add your car sprite/movieclip onto another sprite, where the x and the y coordinates are half the width and height properties. If the car is drawn in adobe flash you can also drag it to the top left, so that the center point is in the middle.

    0 讨论(0)
提交回复
热议问题