开发者

Is every MOUSE_DOWN guaranteed a MOUSE_UP?

开发者 https://www.devze.com 2023-03-20 04:13 出处:网络
Are there any circumstances under which a Flash application could receiv开发者_如何学Pythone two MouseEvent.MOUSE_DOWN without a MouseEvent.MOUSE_UP in between?

Are there any circumstances under which a Flash application could receiv开发者_如何学Pythone two MouseEvent.MOUSE_DOWN without a MouseEvent.MOUSE_UP in between?

In my ActionScript 3 application, I want to track the user dragging a sprite. My plan is to listen for a MouseEvent.MOUSE_DOWN, start an indefinitely repeating timer that records the mouse position, and stop the timer on MouseEvent.MOUSE_UP. Is that a good plan?


  • Click but do not release the button.
  • While keeping the button down move the cursor outside.
  • Release the button outside.
  • Come back inside and click again.

You will get two down event without an up event.

A better solution to drag-and-drop might be to listen MOUSE_MOVE event and in the event handler check buttonDown property. If the button is down while moving, this will be true, otherwise false.

EDIT: For stage even if you release the button outside the flash window then up event is fired. So for stage this can work, but still instead of timer using MOUSE_MOVE with buttonDown looks better to me. You can check yourself with the following code:

public class StageEventTest extends Sprite
{
    public function StageEventTest()
    {
        stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
        stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
    }

    private function onDown(evt:MouseEvent):void {
        trace("down");
    }

    private function onUp(evt:MouseEvent):void {
        trace("up");
    }

    private function onMove(evt:MouseEvent):void {
        trace("move", evt.buttonDown);
    }
}


it will miss the mouse up event if the mouse is outside of the stage and continue it's mouse tracking when mousing over the stage since the mouse up event was never called. this likely problem can be mitigated by using Event.MOUSE_LEAVE - forward the mouse leave event to the mouse up event handler so that if the user mouses away from the stage the mouse up event will be called.


Yes, but unless you are listening to the stage, I think you can miss the mouseup event if the mouse has gone outside the Flash movie. Nothing beats an experiment though.


Below you can find the code I always use for custom dragging. Normally, it should work fine. I listen to the MOUSE_MOVE-event for repositioning and tracking the sprite, but you can just as well use the Event.ENTER_FRAME-event or use a timer instead.

ObjectToDrag.addEventListener(MouseEvent.MOUSE_DOWN, startCustomDrag);

public function startCustomDrag(e:MouseEvent):void
{
    _prevX= e.stageX;
    _prevY= e.stageY;

    stage.addEventListener(MouseEvent.MOUSE_MOVE, dragObject); //REPLACE BY TIMER OR Event.ENTER_FRAME
    stage.addEventListener(MouseEvent.MOUSE_UP, stopCustomDrag);
}

private function dragObject(e:MouseEvent):void
{
    ObjectToDrag.x += _prevX - e.stageX; //OR ANY OTHER CALCULATION
    ObjectToDrag.y += _prevY - e.stageY; //OR ANY OTHER CALCULATION

    _prevX= e.stageX;
    _prevY= e.stageY;
}

public function stopCustomDrag(e:MouseEvent):void
{
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragObject); //REPLACE BY TIMER OR Event.ENTER_FRAME
    stage.removeEventListener(MouseEvent.MOUSE_UP, stopCustomDrag);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号