2013-01-18 117 views
1

我正在使用Flashdevelop設計Haxe NME的遊戲。我在屏幕上有一個對象,我希望它隨着鼠標移動而旋轉以跟隨鼠標。我有物體以與鼠標相同的速度旋轉,但它不指向鼠標。就像我的鼠標移動時屏幕上有一個幻影鼠標一樣。旋轉精靈來跟蹤鼠標點?

這是隻要鼠標改變當前位置被調用的代碼:

public function mouseProcess(e:MouseEvent) 
{ 
    var Xdistance:Float = e.localX - survivor.x; 
    var Ydistance:Float = e.localY - survivor.y; 
    survivor.rotation = Math.atan2(Ydistance, Xdistance) * 180/Math.PI; 
} 

e.localX/Y獲取當前的x,鼠標和倖存者的y位置。 x/y獲取需要旋轉的對象的x,y位置。

謝謝

回答

1

我找不到任何錯誤的方法。我幾乎是用下面的代碼逐字地使用它來設置一個跟蹤鼠標的精靈來移動它。也許看看我寫的內容,看看它與你的代碼有什麼不同。如果沒有,可能會發布更多你所做的事情?

// Creates the sprite that will visually track the mouse. 
private function CreateSurvivor() : Sprite 
{ 
    // Create a green square with a white "turret". 
    var shape = new Shape(); 
    shape.graphics.beginFill(0x00FF00); 
    shape.graphics.drawRect(0, 0, 100, 100); 
    shape.graphics.beginFill(0xFFFFFF);   
    shape.graphics.drawRect(50, 45, 50, 10); 
    shape.graphics.endFill(); 

    // Center the square within its outer container. Allows it to spin 
    // around its center point. 
    shape.x = -50; 
    shape.y = -50; 

    var survivor = new Sprite(); 
    survivor.addChild(shape); 

    return survivor; 
} 

init方法只創建倖存者並將其附加到顯示列表。

private function init(e) 
{ 
    m_survivor = CreateSurvivor(); 
    m_survivor.x = 300; 
    m_survivor.y = 200; 

    addChild(m_survivor); 

    stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseProcess); 
} 

最後,你原來的方法:

public function mouseProcess(e:MouseEvent) : Void 
{ 
    var Xdistance:Float = e.localX - m_survivor.x; 
    var Ydistance:Float = e.localY - m_survivor.y; 
    m_survivor.rotation = Math.atan2(Ydistance, Xdistance) * 180/Math.PI; 
} 

希望這有助於。

1

我不確定在NME中這是否不同,但Flash的Math.atan2()給出的值從0開始指向左側(負x),而顯示對象從0開始向上,因此只需將+ 90你的角度有幫助?

+0

修復它。我知道這可能是我錯過的簡單東西。 – user1989292