2013-01-18 118 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