【问题标题】:actionscript 3 mouse downactionscript 3 鼠标按下
【发布时间】:2011-10-30 18:44:31
【问题描述】:

如何不断检查鼠标是否已按下以及鼠标是否已按下并且它移动了我调用的函数

我试过mk_mc.addEventListener(MouseEvent.MOUSE_DOWN,fct),但它只调用函数然后停止,我想不断地这样做,我该怎么做?

【问题讨论】:

    标签: flash actionscript-3 mouseevent


    【解决方案1】:

    以前有人问过您要的问题,但我无法轻易找到重复的问题,因此我将发布指向 my 问题的链接asking for help with a few niche issues

    在 AS3 中处理拖动事件的一些合理代码是:

    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
    }
    

    【讨论】:

      【解决方案2】:

      那么你想做的是当鼠标移动并且按钮按下时调用一个函数?

      最简单的方法是在鼠标移动时调用该函数(或者如果您想在鼠标不移动时调用该函数,则使用计时器),并让它检查鼠标向上/向下设置的标志它采取任何行动。

      var isDown:Boolean = false;
      
      stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
      stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
      stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
      
      function onMouseDown(evt:MouseEvent):void
      {
          isDown = true;
      }
      
      function onMouseUp(evt:MouseEvent):void
      {
          isDown = false;
      }
      
      function onMouseMove(evt:MouseEvent):void
      {
          if(isDown) {
              //party
          }
      }
      

      【讨论】:

        【解决方案3】:

        我认为您可以在鼠标按下时简单地设置一些标志,并在鼠标升起时再次将其设置回来。类似的东西:

        private var down_:Boolean = false;
        
        mk_mc.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
        
        function onMouseDown(event) {
            down_ = true;   
            mk_mc.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
        }
        
        function onMouseUp(event) {
            down_ = false;  
            mk_mc.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
        }
        

        然后只需轮询down_ 以了解鼠标是否已按下。

        【讨论】:

        • 这段代码有很多微妙的问题。它通常工作得相当好,但在某些情况下它会失败,导致即使在释放鼠标后拖动也会卡住。
        猜你喜欢
        • 1970-01-01
        • 2013-09-29
        • 1970-01-01
        • 2012-09-22
        • 2016-08-25
        • 2010-12-16
        • 2013-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多