检测锁定的可拖动对象上的鼠标移动和释放
我正在制作一个自定义滑块组件。头部(您拖动的东西)的编程如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要在 head 的 MOUSE_MOVE 事件中调用 updateLevel(),而是在 head 的 ENTER_FRAME 事件中调用它:
我之前研究过类似的功能,并且还需要处理 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:
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
在 AS3 中,这是通过向舞台添加一个 MouseEvent 侦听器来侦听 MOUSE_UP 事件来处理的。
正如您所看到的,出于风格问题,我避免在事件侦听器中使用匿名函数。在大多数情况下,这允许更大的灵活性,并且在我看来,更容易阅读。
至于您的其他问题,没有“滚动手柄”对 MOUSE_MOVE 事件做出反应...您是否希望它即使在您滚离手柄后仍然做出反应?根据您希望其如何工作,可以通过多种方式完成。
请准确说明您正在寻找什么行为。
In AS3 this is handled by adding a MouseEvent listener to the stage listening for the MOUSE_UP event.
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.