当 wmode=opaque 或 wmode=transparent 时如何检测 Event.MOUSE_LEAVE

发布于 2024-10-26 14:02:52 字数 1285 浏览 6 评论 0原文

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

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=transparentwmode=opaque 一起使用时,MOUSE_LEAVE<当 MOUSE_UP 事件发生在舞台外时,未检测到 /code> 事件。

wmode透明不透明时,有没有办法检测MOUSE_LEAVE事件?

有没有办法检测wmode设置为透明不透明以便解决方法可以实施吗?

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
}

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.

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

OR

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隐诗 2024-11-02 14:02:52

默认情况下,MOUSE_LEAVE 不是一个可靠的事件。有时它会被解雇,有时则不会。您可以在网络上查找对此的投诉

不过,您可以做一件事,那就是手动检查鼠标是否位于舞台上方:

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 变量以防止事件处理程序的双重执行。

PS 我不确定这是否适用于所有 stage.align 和 stage.scaleMode 选项。它应该适用于 StageScaleMode.NO_SCALE 和 StageAlign.TOP_LEFT 的组合,对于任何其他设置,您必须检查并可能找到解决方法。

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"));
    }
}

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文