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

查看:119
本文介绍了检测鼠标离开舞台,同时拖动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天全站免登陆