【问题标题】:How do I stop mouse_out firing when mouse click fires鼠标单击触发时如何停止mouse_out触发
【发布时间】:2010-10-17 14:31:40
【问题描述】:

我有具有 mouse_over、mouse_out 和 CLICK 事件的按钮。但是当我单击按钮时,它会将我带到另一个框架,并且 mouse_out 事件试图触发。我该如何阻止这种情况发生?

act1_btn.addEventListener(MouseEvent.CLICK, act1Pressed);    
act1_btn.addEventListener(MouseEvent.MOUSE_OVER, act1Over);
act1_btn.addEventListener(MouseEvent.MOUSE_OUT, act1Out);
act1_btn.addEventListener(Event.ENTER_FRAME, act1EnterFrame);



function act1Over(e:MouseEvent):void 
 {
  trace("over");
  act1Animating = true; 
  logo_1.visible = true;
  bubble.visible = true;
  txt1.visible = true;
 }

 function act1Out(e:MouseEvent):void 
 {
  act1Animating = false;
  logo_1.visible = false;
  bubble.visible = false;
  txt1.visible = false;
 }

 function act1EnterFrame(e:Event):void
 {
  if (act1Animating && e.target.scaleY < 1.1)
  {
   e.target.scaleY += 0.02;
   e.target.scaleX += 0.02;

  } 

  if (!act1Animating && e.target.scaleY > 1)
  {
   e.target.scaleY -= 0.02;
   e.target.scaleX -= 0.02;
  } 
 }

 function act1Pressed(e:MouseEvent):void 
 {
  trace("clicked");
  act1Animating = false;
  logo_1.visible = false;
  bubble.visible = false;
  txt1.visible = false;
  gotoAndStop(2);
 }

【问题讨论】:

    标签: actionscript-3 mouse click out


    【解决方案1】:

    这里有两种处理方法:

    1) 仅在 MOUSE_OVER 处理程序中分配 MOUSE_OUT 侦听器,然后在 MOUSE_OUT 处理程序完成后将其删除。即,

    function act1Over(e:MouseEvent):void {
      /* your code */
      act1_btn.addEventListener(MouseEvent.MOUSE_OUT, act1Out); 
    }
    
    function act1Out(e:MouseEvent):void {
      /* your code */
      act1_btn.removeEventListener(MouseEvent.MOUSE_OUT, act1Out); 
    }
    

    2) 在您的 CLICK 处理程序中使用 stopPropagation():

    function act1Pressed(e:MouseEvent):void {
      /* your code */
      e.stopPropagation();
    }
    

    另外,今后请使用代码标签来标记您的代码!

    【讨论】:

      【解决方案2】:

      试试 ROLL_OVER 和 ROLL_OUT MouseEvent 可能不是一个坏主意。当有人翻过物体或滚出时,它们只会发射一次,而不是连续发射。

      【讨论】:

      • ROLL_OVER 和 ROLL_OUT 为我解决了这个问题。谢谢。
      【解决方案3】:

      当你点击一个按钮时,你会触发一个 MouseOver 和一个 MouseOut 事件,如果你不想在 Click 事件之后触发 MouseOut 事件,那么你应该在 Click 事件监听器中移除 MouseOut 事件监听器。

      这意味着为了确保您在 MouseOver 时有一个 MouseOut 侦听器,您应该在 MouseOver 侦听器中添加您的 MouseOut 侦听器。

      最后,您应该在 MouseOut 侦听器中删除 MouseOut 事件侦听器。

      【讨论】:

        猜你喜欢
        • 2011-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-15
        • 1970-01-01
        相关资源
        最近更新 更多