Actionscript 3 将对象沿鼠标方向旋转3
我试图在拖动鼠标时使对象沿鼠标方向旋转。例如,我希望汽车指向被拖动的方向。我现在只有拖放代码Actionscript 3 将对象沿鼠标方向旋转3,actionscript-3,Actionscript 3,我试图在拖动鼠标时使对象沿鼠标方向旋转。例如,我希望汽车指向被拖动的方向。我现在只有拖放代码 car.addEventListener(MouseEvent.MOUSE_DOWN, pickUp); car.addEventListener(MouseEvent.MOUSE_UP, dropIt); function pickUp(event:MouseEvent):void { event.target.startDrag(true); event.target.pare
car.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
car.addEventListener(MouseEvent.MOUSE_UP, dropIt);
function pickUp(event:MouseEvent):void {
event.target.startDrag(true);
event.target.parent.addChild(event.target);
}
function dropIt(event:MouseEvent):void {
event.target.stopDrag();
}
一旦开始拖动对象,就可以开始侦听
MouseMove
事件。执行此操作时,将当前鼠标位置与上一个鼠标位置进行比较,并确定两者之间的角度。然后,将该角度用作对象的旋转:
car.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
car.addEventListener(MouseEvent.MOUSE_UP, dropIt);
var oldPoint:Point;
function pickUp(event:MouseEvent):void
{
event.target.startDrag(true);
event.target.parent.addChild(event.target);
oldPoint = new Point(mouseX, mouseY);
// start listening to mouse move events
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
function dropIt(event:MouseEvent):void
{
oldPoint = null;
event.target.stopDrag();
// stop listening to mouse move events
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
function onMouseMove(event:MouseEvent):void
{
if(!oldPoint)
{
return;
}
var newPoint:Point = new Point(mouseX, mouseY);
// get the angle between the two points and set it as the rotation
car.rotation = point_direction(oldPoint, newPoint);
}
function point_direction(p1:Point, p2:Point):Number
{
return Math.atan2(p2.y - p1.y, p2.x - p1.x) * (180 / Math.PI);
}
注意:(根据@Vesper的评论)当鼠标从左向右移动时,将产生0的旋转。这意味着您希望
汽车的图形朝右。谢谢Marcela!只有一个问题。当我拖动汽车时,汽车的侧面是引导点,而不是前发动机罩。它可能很小,但我对flash不太熟悉。你需要做两件事中的一件:偏移从point\u direction
方法获得的旋转,或者重新确定汽车对象图形的方向,使注册点位于正确的中心。参见图片:谢谢Marcela!那是一个大问题help@Marcela你应该说0旋转是朝右而不是朝上的,因此更好的解决方案是旋转汽车MC,使其在设计器中朝右。