如何在AS3点击之间加一个冷却时间?

时间:2013-07-27 04:01:08

标签: actionscript-3 timer click mouse

所以,例如,如果你做了stage.addEventListener(MouseEvent.CLICK, onClick); - 你怎么能设置...让我们说5秒的冷却时间,所以你必须等待5秒才能让你再次点击?或者至少运行onClick函数。

2 个答案:

答案 0 :(得分:2)

选项#1:

stage.addEventListener(MouseEvent.CLICK, onClick);

private function onClick(e:MouseEvent):void
{
    stage.removeEventListener(MouseEvent.CLICK, onClick);
    setTimeout(function() { 
        stage.addEventListener(MouseEvent.CLICK, onClick);
    }, 5000);
} 

选项#2:

stage.addEventListener(MouseEvent.CLICK, onClick);
var then:int = 0;
private function onClick(e:MouseEvent):void
{   
    var now:int = getTimer();
    if(now - then > 5000) {
       // do your thing
       then = now;
    }
} 

选项#3:

stage.addEventListener(MouseEvent.CLICK, onClick);
var timer:Timer = new Timer(5000, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
private function onClick(e:MouseEvent):void
{   
   stage.removeEventListener(MouseEvent.CLICK, onClick);
   timer.start();  
} 

private function onComplete(e:TimerEvent):void
{ 
    stage.addEventListener(MouseEvent.CLICK, onClick);
    timer.reset();
}

答案 1 :(得分:0)

我处理这个问题的方法是创建一个全局的TouchManager。我讨厌实践中的全局对象,但在某些情况下,它们很好地解决了这个问题。在这种情况下,全局是有意义的,因为就我的目的而言,我希望禁用所有可能来源的所有触摸输入。

在我的TouchManager中,有一个静态函数允许任何调用者暂停计时器的可触摸性等。在我的实现中,我将调用此函数超时5秒。然后,任何处理触摸输入的事件处理程序都会咨询TouchManager,如果当前暂停触摸处理,则会提前退出处理程序。

提示"如果某事被称为经理,那么你做错了"人群。