簡體   English   中英

在Actionscript 3.0中圍繞中心點完美旋轉

[英]Perfect rotation around centre point in Actionscript 3.0

我試圖讓一個物體繞其中心點旋轉,但我注意到物體在旋轉過程中遠離其原始點。 這是我的代碼:

import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Shape;


var rectangle:Shape = new Shape  ;
var timer:Timer = new Timer(1,360);

rectangle.graphics.beginFill(0xFF0000);
rectangle.graphics.drawRect(0, 0, 100,100);
rectangle.graphics.endFill();
rectangle.x = 200;
rectangle.y = 200;
addChild(rectangle);

timer.addEventListener(TimerEvent.TIMER,rotateNOW);
timer.addEventListener(TimerEvent.TIMER_COMPLETE,rotationDone);

function rotateNOW(e:TimerEvent)
{
    var matrix:Matrix = rectangle.transform.matrix;
    var rect:Rectangle = rectangle.getBounds(rectangle.parent);
    matrix.translate(- (rect.left + (rect.width/2)), - (rect.top + (rect.height/2)));
    matrix.rotate((1/180)*Math.PI);
    matrix.translate(rect.left + (rect.width / 2), rect.top + (rect.height / 2));
    rectangle.transform.matrix = matrix;
}
function rotationDone(e:TimerEvent)
{
    timer.reset();
    timer.start();
    trace("x:",rectangle.x,"y:",rectangle.y); //to observe the x y changes

}

timer.start();

有沒有辦法旋轉沒有問題?

嗯,我懷疑是否可能因精度損失而導致細微變化?

由於您從矩形中獲取矩陣,調整矩陣​​,然后反復重新應用矩陣,可能會導致精度損失,從而導致移位。

如果我將旋轉設置為90度的數字(導致沒有小數的東西?),那么轉移就會消失。

如果您堅持以這種方式進行旋轉(通過從顯示對象中讀取矩陣,轉換它,然后重新應用它),不確定如何解決這個問題...但是還有其他方法可以旋轉形狀。 我能想到的最簡單的方法是使用DisplayObject的rotation屬性。

下面的示例如下(從您的代碼更改):

import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Shape;

var rectangle:Shape = new Shape();
var timer:Timer = new Timer(1,360);

rectangle.graphics.beginFill(0xFF0000);
// shifted the x and y by half negative width and height so 0, 0 is at the center
rectangle.graphics.drawRect(-50, -50, 100, 100);
rectangle.graphics.endFill();
rectangle.x = 200;
rectangle.y = 200;
addChild(rectangle);

timer.addEventListener(TimerEvent.TIMER,rotateNOW);
timer.addEventListener(TimerEvent.TIMER_COMPLETE,rotationDone);

function rotateNOW(e:TimerEvent):void {
    rectangle.rotation++;
}

function rotationDone(e:TimerEvent):void {
    timer.reset();
    timer.start();
    trace("x:",rectangle.x,"y:",rectangle.y); //to observe the x y changes
}

timer.start();

暫無
暫無

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

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