当wmode = opaque或wmode = transparent时,如何检测Event.MOUSE_LEAVE [英] How to detect Event.MOUSE_LEAVE when wmode=opaque or wmode=transparent

查看:194
本文介绍了当wmode = opaque或wmode = transparent时,如何检测Event.MOUSE_LEAVE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的拖动事件,适用于大多数事情:

I have a customized drag event that works great for most things:

stage.addEventListener( MouseEvent.MOUSE_DOWN, beginDrag );

function beginDrag( e:MouseEvent )
{
  stage.addEventListener( MouseEvent.MOUSE_MOVE, drag );
  stage.addEventListener( MouseEvent.MOUSE_UP, endDrag );
  stage.addEventListener( MouseEvent.DEACTIVATE, endDrag );
  stage.addEventListener( Event.MOUSE_LEAVE, endDrag );
  stage.addEventListener( Event.REMOVED_FROM_STAGE, stageEndDrag );

  //trigger beginDrag event
}
function drag( e:MouseEvent )
{
  //trigger drag event
}
function endDrag( e:Event )
{
  stage.removeEventListener( MouseEvent.MOUSE_MOVE, drag );
  stage.removeEventListener( MouseEvent.MOUSE_UP, endDrag );
  stage.removeEventListener( MouseEvent.DEACTIVATE, endDrag );
  stage.removeEventListener( Event.MOUSE_LEAVE, endDrag );
  stage.removeEventListener( Event.REMOVED_FROM_STAGE, stageEndDrag );

  //trigger endDrag event
}

问题是当我使用这个代码与 wmode = transparent wmode = opaque MOUSE_LEAVE MOUSE_UP 事件发生在舞台上时,未检测到code>事件。

The issue is that when I use this code with wmode=transparent or wmode=opaque the MOUSE_LEAVE event is not detected when the MOUSE_UP event occurs off the stage.

有没有办法当 wmode 透明时,检测 MOUSE_LEAVE code> opaque ?

Is there a way to detect the MOUSE_LEAVE event when wmode is transparent or opaque?

OR

有没有办法检测到 wmode 设置为 transparent opaque ,以便可以实现解决方案?

Is there a way to detect that the wmode is set to transparent or opaque so that a work-around may be implemented?

推荐答案

默认情况下,MOUSE_LEAVE不是可靠的事件。有时它被解雇,在其他时候它不会。您可以在这个网站上查找关于这个的投诉

By default, MOUSE_LEAVE is not a reliable event. Sometimes it gets fired, at other times it won't. You can find complaints about this all over the web.

虽然有一件事情可以做,手动检查鼠标是否在舞台上:

There is one thing you can do, though, and that is to manually check if the mouse is over the stage:

var out : Boolean = false;

stage.addEventListener (Event.ENTER_FRAME, checkMouse);

function checkMouse (ev:Event) : void {
    if (
        stage.mouseX < 0 || 
        stage.mouseX > stage.stageWidth || 
        stage.mouseY < 0 || 
        stage.mouseY > stage.stageHeight) 
    {
        if (!out) 
        {
            out = true;
            stage.dispatchEvent (new Event(Event.MOUSE_LEAVE));
        }
    } 
    else if (out) 
    {
        out = false;
        stage.dispatchEvent (new Event("mouseEnter"));
    }
}

当光标在外面时,这将发送MOUSE_LEAVE事件的舞台区域,以及自定义的mouseEnter事件,当它重新进入。然后,您可以向舞台添加事件侦听器以对这些事件做出可靠的反应,但是您必须记住,一次可能会触发多个MOUSE_LEAVE(如果自定义的和内置的一个都被执行)。您可以检查 out 变量,以防止双重执行事件处理程序。

This will dispatch the MOUSE_LEAVE event when the cursor is outside of the stage area, and the custom "mouseEnter" event, when it reenters. You can then add event listeners to the stage to reliably react to these events, but you have to keep in mind that more than one MOUSE_LEAVE might be fired at a time (if both the custom one and the built-in one are executed). You can check the out variable to prevent double execution of the event handlers.

P.S。我不知道这适用于所有stage.align和stage.scaleMode选项。它应该适用于StageScaleMode.NO_SCALE和StageAlign.TOP_LEFT的组合,对于您将要检查的任何其他设置,并可能找到解决方法。

P.S. I am not sure this works for all stage.align and stage.scaleMode options. It should work for the combination of StageScaleMode.NO_SCALE and StageAlign.TOP_LEFT, for any other settings you will have to check and possibly find a workaround.

这篇关于当wmode = opaque或wmode = transparent时,如何检测Event.MOUSE_LEAVE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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