检测锁定的可拖动对象上的鼠标移动和释放

发布于 2024-09-13 21:10:24 字数 643 浏览 3 评论 0原文

我正在制作一个自定义滑块组件。头部(您拖动的东西)的编程如下:

        head.addEventListener(MouseEvent.MOUSE_DOWN, function():void {
            head.startDrag(false, new Rectangle(stubDiv,0,width - stubDiv - ((levels-maxLevel)*stubDiv),0));
        });
        head.addEventListener(MouseEvent.MOUSE_MOVE, function():void {
            updateLevel();
        });
        head.addEventListener(MouseEvent.MOUSE_UP, function():void {
            head.stopDrag();
            setHeadPos();
        });

由于头部被限制在滑块区域,因此鼠标可以远离它。如果发生这种情况,对象仍在被拖动,但它不会接收 MOUSE_MOVE 事件,如果释放鼠标,也不会接收 MOUSE_UP 事件。

最好的解决方案是什么?

I'm making a custom slider component. The head (the thing that you drag) is programmed like this:

        head.addEventListener(MouseEvent.MOUSE_DOWN, function():void {
            head.startDrag(false, new Rectangle(stubDiv,0,width - stubDiv - ((levels-maxLevel)*stubDiv),0));
        });
        head.addEventListener(MouseEvent.MOUSE_MOVE, function():void {
            updateLevel();
        });
        head.addEventListener(MouseEvent.MOUSE_UP, function():void {
            head.stopDrag();
            setHeadPos();
        });

Because the head is constrained to the area of the slider bar, the mouse can move away from it. If that happens, the object is still being dragged, but it doesn't receive MOUSE_MOVE events, nor the MOUSE_UP event if the mouse is released.

What's the best solution?

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

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

发布评论

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

评论(2

好倦 2024-09-20 21:10:24

不要在 head 的 MOUSE_MOVE 事件中调用 updateLevel(),而是在 head 的 ENTER_FRAME 事件中调用它:

head.addEventListener(MouseEvent.MOUSE_DOWN, function():void { 
    head.startDrag(false, new Rectangle(stubDiv,0,width - stubDiv - ((levels-maxLevel)*stubDiv),0)); 
    head.addEventListener(Event.ENTER_FRAME, function():void  { updateLevel(); });
}); 

head.addEventListener(MouseEvent.MOUSE_UP, function():void { 
    head.stopDrag(); 
    head.removeEventListener(Event.ENTER_FRAME, function():void  { updateLevel(); });
    setHeadPos(); 
}); 

head.Stage.addEventListener(Event.MOUSE_LEAVE, function():void { 
    head.stopDrag(); 
    head.removeEventListener(Event.ENTER_FRAME, function():void  { updateLevel(); });
    setHeadPos(); 
}); 

我之前研究过类似的功能,并且还需要处理 Stage 的 Mouse_Leave。根据您的要求,您可能还想将 MouseEvent.MOUSE_OUT 添加到 head

Instead of calling updateLevel() in head's MOUSE_MOVE event, call it in the head's ENTER_FRAME event:

head.addEventListener(MouseEvent.MOUSE_DOWN, function():void { 
    head.startDrag(false, new Rectangle(stubDiv,0,width - stubDiv - ((levels-maxLevel)*stubDiv),0)); 
    head.addEventListener(Event.ENTER_FRAME, function():void  { updateLevel(); });
}); 

head.addEventListener(MouseEvent.MOUSE_UP, function():void { 
    head.stopDrag(); 
    head.removeEventListener(Event.ENTER_FRAME, function():void  { updateLevel(); });
    setHeadPos(); 
}); 

head.Stage.addEventListener(Event.MOUSE_LEAVE, function():void { 
    head.stopDrag(); 
    head.removeEventListener(Event.ENTER_FRAME, function():void  { updateLevel(); });
    setHeadPos(); 
}); 

I worked on a similar feature before and I also needed to handle Stage's Mouse_Leave. Depending on your requirement, you might also want to add MouseEvent.MOUSE_OUT to head

红颜悴 2024-09-20 21:10:24

在 AS3 中,这是通过向舞台添加一个 MouseEvent 侦听器来侦听 MOUSE_UP 事件来处理的。

head.addEventListener(MouseEvent.MOUSE_DOWN, _handleDown);

function _handleDown($evt:MouseEvent):void {
    // add a MouseEvent.MOUSE_UP listener to the stage
    stage.addEventListener(MouseEvent.MOUSE_UP, _handleUp);
    head.removeEventListener(MouseEvent.MOUSE_DOWN, _handleDown);
    head.addEventListener(MouseEvent.MOUSE_MOVE, _handleMove);
    head.addEventListener(MouseEvent.MOUSE_UP, _handleUp);
    head.startDrag(false, new Rectangle(stubDiv,0,width - stubDiv - ((levels-maxLevel)*stubDiv),0)
}

function _handleMove($evt:MouseEvent):void {
    updateLevel();
}

function _handleUp($evt:MouseEvent):void {
    // remove the MouseEvent.MOUSE_UP listener from the stage
    stage.removeEventListener(MouseEvent.MOUSE_UP, _handleUp);
    head.removeEventListener(MouseEvent.MOUSE_UP, _handleUp);
    head.removeEventListener(MouseEvent.MOUSE_MOVE, _handleMove);
    head.addEventListener(MouseEvent.MOUSE_DOWN, _handleDown);
    head.stopDrag();
    setHeadPos();
}

正如您所看到的,出于风格问题,我避免在事件侦听器中使用匿名函数。在大多数情况下,这允许更大的灵活性,并且在我看来,更容易阅读。

至于您的其他问题,没有“滚动手柄”对 MOUSE_MOVE 事件做出反应...您是否希望它即使在您滚离手柄后仍然做出反应?根据您希望其如何工作,可以通过多种方式完成。

请准确说明您正在寻找什么行为。

In AS3 this is handled by adding a MouseEvent listener to the stage listening for the MOUSE_UP event.

head.addEventListener(MouseEvent.MOUSE_DOWN, _handleDown);

function _handleDown($evt:MouseEvent):void {
    // add a MouseEvent.MOUSE_UP listener to the stage
    stage.addEventListener(MouseEvent.MOUSE_UP, _handleUp);
    head.removeEventListener(MouseEvent.MOUSE_DOWN, _handleDown);
    head.addEventListener(MouseEvent.MOUSE_MOVE, _handleMove);
    head.addEventListener(MouseEvent.MOUSE_UP, _handleUp);
    head.startDrag(false, new Rectangle(stubDiv,0,width - stubDiv - ((levels-maxLevel)*stubDiv),0)
}

function _handleMove($evt:MouseEvent):void {
    updateLevel();
}

function _handleUp($evt:MouseEvent):void {
    // remove the MouseEvent.MOUSE_UP listener from the stage
    stage.removeEventListener(MouseEvent.MOUSE_UP, _handleUp);
    head.removeEventListener(MouseEvent.MOUSE_UP, _handleUp);
    head.removeEventListener(MouseEvent.MOUSE_MOVE, _handleMove);
    head.addEventListener(MouseEvent.MOUSE_DOWN, _handleDown);
    head.stopDrag();
    setHeadPos();
}

As you can see, as a matter of style, I avoid using anonymous functions in my event listeners. This allows for far more flexibility in most cases, and in my opinion, is far easier to read.

As far as your other issue, not having the "scroll handle" react to MOUSE_MOVE events... do you want it to still react even after you have rolled away from the handle? Depending on how you want this to work it could be done a variety of ways.

Please clarify exactly what behavior you are looking for.

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