在 Actionscript 3 中拖动时检测鼠标离开舞台 [英] Detect Mouse leave stage while dragging in Actionscript 3

查看:27
本文介绍了在 Actionscript 3 中拖动时检测鼠标离开舞台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Event.MOUSE_LEAVE 是在 Actionscript 3 中很棒,但如果用户按住左(或右)鼠标按钮,它似乎不会触发.

Event.MOUSE_LEAVE is great in Actionscript 3, but it doesn't seem to fire if the user is holding their left (or right for that matter) mouse button down.

有没有办法在按住鼠标的同时检测鼠标是否离开 Flash 影片?或者如果它是在Flash电影之外发布的?

Is there a way to detect if the mouse leaves the Flash movie while the mouse is held down? Or if it is released outside the flash movie?

推荐答案

要获得所有这些,需要一点技巧.您必须存储鼠标是否离开舞台并相应地处理 Event.MOUSE_LEAVE 事件.这样做可以为您提供所有正常的鼠标功能,包括不会因为鼠标离开舞台而停止拖动.由于用户可能会回到舞台并继续拖动,它会等待直到用户在舞台上或舞台外松开鼠标.

To get all of that requires a little bit of a hack. You have to store whether the mouse is off the stage or not and handle the Event.MOUSE_LEAVE event accordingly. Doing it this way gives you all the normal mouse functionality including not stopping the drag just because the mouse went off stage. Since the user might come back on stage and continue the drag it waits 'til the user releases the mouse either on or off stage.

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 事件,在鼠标离开舞台时被触发,所以你必须跟踪是否鼠标被释放时已经离开了舞台.

The hack is that the MOUSE_LEAVE event, not the MOUSE_UP event, gets fired when the mouse is released off stage so you have to keep track of whether or not the mouse was already off stage when it was released.

在拖动完成后,您当然希望删除与检测鼠标移开和鼠标移开相关的所有事件侦听器,但为了可读性而省略了该代码.

after the drag is finished you of course want to remove all the event listeners associated with detecting mouse-outs and mouse-ups but that code was left out for readability.

这篇关于在 Actionscript 3 中拖动时检测鼠标离开舞台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆