拖动,按照一般人的定义,拖动就是鼠标按下的时候移动鼠标,这里面有三个过程,分别是按下、移动鼠标和弹起。以stage为例,大家的实现步骤通常如下: (PS:此处不讨论startDrag和stopDrag的使用,因为这个方法过于局限,诸如切水果一类的,startDrag能做的到么) 第一步,监听鼠标按下事件,并开始监听移动,同时为了让弹起时不再拖动,我们还要监听弹起事件:
stage.addEventListener(MouseEvent.MOUSE_DOWN, stageMouseDownHandler function stageMouseDownHandler(event:MouseEvent):void{ stage.addEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler); stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler); }
第二步,在stageMouseMoveHandler里执行拖动的操作
function stageMouseMoveHandler(event:MouseEvent):void{ //在这里运行拖动相关的代码 }
function stageMouseUpHandler(event:MouseEvent):void{ stage.removeEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler); stage.removeEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler); }
stage.addEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler); function stageMouseMoveHandler(event:MouseEvent):void{ if(event.buttonDown){ //在这里运行拖动相关的代码 } }