簡體   English   中英

AS3 圍繞其中心點旋轉對象

[英]AS3 Rotate an object around its center point

我希望這個對象圍繞它的中心而不是左上角旋轉。 代碼如下所示:

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

所以當我按下左鍵時,汽車向左轉,但現在它跳起來了一點,因為它繞着頂角旋轉。

謝謝

以下將圍繞 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);
    var centerX = rect.left + (rect.width / 2);
    var centerY = rect.top + (rect.height / 2);
    matrix.translate(-centerX, -centerY);
    matrix.rotate((angleDegrees / 180) * Math.PI);
    matrix.translate(centerX, centerY);
    object.transform.matrix = matrix;
    
    object.rotation = Math.round(object.rotation);
}
    

它將對象的中心平移到 0,0,然后旋轉它,然后將其平移回來。

完成此操作的最簡單方法是將您的汽車精靈/影片剪輯添加到另一個精靈上,其中 x 和 y 坐標是寬度和高度屬性的一半。 如果汽車是在 adobe flash 中繪制的,您也可以將它拖到左上角,使中心點在中間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM