Flash 旋转精灵以跟随鼠标点?
我正在使用Flashdevelop在Haxe NME中设计一个游戏。我在屏幕上有一个对象,我希望它随着鼠标移动而旋转。我让物体以与鼠标相同的速度旋转,但它并不指向鼠标。就像屏幕上有一个幻影鼠标,每当我的鼠标移动时,它就会移动 这是每当鼠标改变位置时调用的代码:Flash 旋转精灵以跟随鼠标点?,flash,rotation,mouse,point,haxe,Flash,Rotation,Mouse,Point,Haxe,我正在使用Flashdevelop在Haxe NME中设计一个游戏。我在屏幕上有一个对象,我希望它随着鼠标移动而旋转。我让物体以与鼠标相同的速度旋转,但它并不指向鼠标。就像屏幕上有一个幻影鼠标,每当我的鼠标移动时,它就会移动 这是每当鼠标改变位置时调用的代码: public function mouseProcess(e:MouseEvent) { var Xdistance:Float = e.localX - survivor.x; var Ydistance:Float
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位置
谢谢我没有发现你的方法有任何问题。我将它(几乎)逐字地与下面的代码一起使用,以设置一个在移动鼠标时跟踪鼠标的精灵。也许看看我写的东西,看看它是否与您的代码不同。如果做不到这一点,也许可以发布更多你所做的事情
// 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;
}
希望这有帮助。我不确定这在NME中是否有所不同,但Flash的
Math.atan2()
给出的值从0开始指向左侧(负x),而显示对象从0开始指向上方,因此只需将+90
添加到角度帮助中即可解决此问题。修复了此问题。我知道我可能错过了一些简单的东西。