舞台上的红色广场。鼠标按下+鼠标拖动将向下移动红色方块(+ y),而鼠标按下+鼠标向下拖动将向上移动红色方块(-y)。需要这种相反的动作。
然而,在鼠标拖动过程中,无论有多少鼠标拖动改变了它的初始化位置,方块必须从它的当前y位置开始移动。目前,红色方块将始终在第0阶段开始,因为我的mouseDownOrigin变量不正确,因为我的大脑已经睡着了。
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Test extends Sprite
{
private var sp:Sprite = new Sprite();
private var mouseDownOrigin:int;
public function Test()
{
sp.graphics.beginFill(0xFF0000);
sp.graphics.drawRect(0, 0, 100, 100);
sp.x = sp.y = 200;
addChild(sp);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventHandler);
}
private function mouseDownEventHandler(evt:MouseEvent):void
{
mouseDownOrigin = evt.stageY;
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
}
private function mouseMoveEventHandler(evt:MouseEvent):void
{
sp.y = mouseDownOrigin - evt.stageY;
}
private function mouseUpEventHandler(evt:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventHandler);
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
}
}
}
答案 0 :(得分:2)
您需要在MouseUp事件处理程序上记录红色方块y位置,实际记录您离开方块的位置。
private var currentPosition:int;
private function mouseUpEventHandler(evt:MouseEvent):void
{
currentPosition = sp.y;
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventHandler);
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
}
private function mouseMoveEventHandler(evt:MouseEvent):void
{
var n:int = event.stageY - mouseDownOrigin ;
sp.y = currentPosition - n;
}