检测鼠标离开舞台,而在ActionScript 3的拖曳 [英] Detect Mouse leave stage while dragging in Actionscript 3

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

问题描述

<一个href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Stage.html#event%3AmouseLeave">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.

在拖动完成后,你当然要删除所有与检测鼠标奏和鼠标起坐但code被排除在外的可读性关联的事件侦听器。

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天全站免登陆