【问题标题】:My mouse clicks created and triggered my eventListener at the same time我的鼠标点击同时创建并触发了我的 eventListener
【发布时间】:2013-06-05 15:19:48
【问题描述】:

我尝试实现以下目标,但使用以下代码失败:

  1. 单击鼠标一次以显示该框。
  2. 再次单击它会消失该框。

发生的情况是,当我触发 mouse.click 事件(通过单击)时,它也触发了“stage.addEventListener(MouseEvent.CLICK, boxGone)”事件侦听器。在屏幕上没有发生任何事情,因为我在技术上 addChild 和 removeChild 框在同一帧。

我猜我的初始点击同时创建并触发了事件监听器。有没有办法在不更改触发事件(鼠标点击)的情况下避免这种情况发生?下面是代码:

public function ClassConstructor(){
 addEventListener(MouseEvent.CLICK, onMouseClickHandler);
}

private function onMouseClickHandler(e:MouseEvent):void{

 box.x = stage.mouseX;
 box.y = stage.mouseY;
 box.gotoAndPlay(1);

 stage.addChild(box);
 stage.addEventListener(MouseEvent.CLICK, boxGone);

}

private function boxGone(e:MouseEvent):void{
 stage.removeChild(box);
 stage.removeEventListener(MouseEvent.CLICK, boxGone);
}

提前致谢, 晴天

【问题讨论】:

    标签: actionscript-3 events


    【解决方案1】:

    修改你的第一个监听器:

    stage.addEventListener(MouseEvent.CLICK, onMouseClickHandler);
    

    事件从你的主类传到舞台,你在中间添加了第二个监听器,所以它在函数关闭之后被调用。可以肯定的是,另一种解决方案是调用

    e.stopImmediatePropagation();
    

    这可以防止任何侦听器捕获相同的事件。

    【讨论】:

      猜你喜欢
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      相关资源
      最近更新 更多