Event.MOUSE_LEAVE在Actionscript 3中非常棒,但如果用户按住左侧(或右侧)鼠标按钮,它似乎不会触发.
有没有办法检测鼠标在按住鼠标时是否离开Flash影片?或者如果它是在flash电影之外发布的?
要获得所有这些,需要一点点破解.您必须存储鼠标是否离开舞台并相应地处理Event.MOUSE_LEAVE
事件.这样做可以为您提供所有正常的鼠标功能,包括不会因为鼠标离开舞台而停止拖动.由于用户可能回到舞台并继续拖动,因此等待用户在舞台上或舞台外释放鼠标.
var mouseOffStage:Boolean; var bonk:YourDisplayObject = new YourDisplayObject() addChild(bonk); bonk.addEventListener(MouseEvent.MOUSE_DOWN, function():void { mouseOffStage = false; bonk.startDrag(); stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp); stage.addEventListener(Event.MOUSE_LEAVE, mouseLeave); stage.addEventListener(MouseEvent.MOUSE_OUT, mouseOut); stage.addEventListener(MouseEvent.MOUSE_OVER, mouseOver); }) private function mouseUp(e:MouseEvent) :void { trace("Mouse Up On Stage") bonk.stopDrag() } private function mouseLeave(e:Event) :void { if(mouseOffStage){ trace("mouse up and off stage"); bonk.stopDrag(); }else{ trace("mouse has left the stage"); //no reason to stop drag here as the user hasn't released the mouse yet } } private function mouseOut(e:MouseEvent) :void { mouseOffStage = true; trace("mouse has left the stage") } private function mouseOver(e:MouseEvent) :void { mouseOffStage = false; trace("mouse has come back on stage"); }
黑客是当鼠标从舞台上释放时,MOUSE_LEAVE
事件而不是MOUSE_UP
事件被触发,因此你必须跟踪鼠标是否已经离开阶段.
拖动完成后,您当然希望删除与检测鼠标输出和鼠标悬停相关的所有事件监听器,但该代码被遗漏以便于阅读.