Actionscript 3 AS3 timer.reset();

Actionscript 3 AS3 timer.reset();,actionscript-3,flash,timer,Actionscript 3,Flash,Timer,从我读到的关于as3定时器的所有信息中。。假设timer.stop()停止计数,并在创建timer.start()时继续计数。另外,我已经读到timer.reset()与stop方法不同,它将计时器恢复到开始计数,以便在点击timer.start reach之前再次从零开始。。。但它不起作用。我一定错过了一些非常基本的东西,因为下面的代码只会停止计数,不会重置为零: import flash.utils.Timer; import flash.events.TimerEvent; import

从我读到的关于as3定时器的所有信息中。。假设timer.stop()停止计数,并在创建timer.start()时继续计数。另外,我已经读到timer.reset()与stop方法不同,它将计时器恢复到开始计数,以便在点击timer.start reach之前再次从零开始。。。但它不起作用。我一定错过了一些非常基本的东西,因为下面的代码只会停止计数,不会重置为零:

import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;

var myTimer:Timer = new Timer(1000);
var time = 0;
myTimer.start()
myTimer.addEventListener(TimerEvent.TIMER,timerHandle);

function timerHandle(e:TimerEvent){
    txt_time.text = time.toString();
    time++;
}

box_restart.addEventListener(MouseEvent.CLICK, checkforclose);

function checkforclose(event:MouseEvent):void
{                           
    if (event.target.name == "box_restart")
    {   
        trace("this function worked");
        myTimer.reset();
        box_restart.removeEventListener(MouseEvent.CLICK, checkforclose);
    }
}

根据您关于停止计时器时文本字段未返回0的评论,请尝试以下代码。它应该能解决你的问题

import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;

var myTimer:Timer = new Timer(1000);
var time = 0;
myTimer.start()
myTimer.addEventListener(TimerEvent.TIMER,timerHandle);

function timerHandle(e:TimerEvent){
    txt_time.text = time.toString();
    time++;
}

box_restart.addEventListener(MouseEvent.CLICK, checkforclose);

function checkforclose(event:MouseEvent):void
{                           
    if (event.target.name == "box_restart")
    {   
        trace("this function worked");
        myTimer.reset();
        txt_time.text = 0;
        box_restart.removeEventListener(MouseEvent.CLICK, checkforclose);
    }
}

对@Anil Natha的解决方案进行调整,您可以使用计时器的currentCount属性,可以从单击处理程序手动调用timerHandler,将txt_时间的更新保持在一个位置

import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;

var myTimer:Timer = new Timer(1000);
myTimer.start()
myTimer.addEventListener(TimerEvent.TIMER,timerHandle);

function timerHandle(e:TimerEvent){
    txt_time.text = myTimer.currentCount.toString();
}

box_restart.addEventListener(MouseEvent.CLICK, checkforclose);

function checkforclose(event:MouseEvent):void
{                           
    if (event.target.name == "box_restart")
    {   
        trace("this function worked");
        myTimer.reset();
        timerHandle(null);
        box_restart.removeEventListener(MouseEvent.CLICK, checkforclose);
    }
}

您对“将计时器恢复到开始计数,使其再次从零开始”的理解是什么?reset():void-停止正在运行的计时器,并将currentCount属性设置回0,就像秒表的reset按钮一样。=>所以它只是将属性设置回0,这并不意味着stop()会在reset()停止计时器时暂停计时器。@Sly Raskal-一定不是范围问题,因为它出现了[object timer]。。。还有其他想法吗?@Fygo-当我单击按钮(框_restart)时,动态文本框中的运行数字停止,但不会返回到0,即使代码显示reset()和not stop()。。。。有什么想法吗?你没有在帖子中提到你的文本字段没有返回0。如果这是你的问题,你需要更清楚。如果您希望在计时器停止时文本字段返回0,只需在
checkForClose
事件处理程序中将文本字段设置为0即可。是的,这肯定是此问题的答案。我重新创建了一个swf的一部分,我遇到了麻烦,并试图缩小问题的范围。。我原以为问题出在重置上,但正如您刚才所示,它不是。当定时器在swf上被中断时,我无法获得定时器显示的图像,以设置回alpha=0,我不知道为什么。。。至少我可以排除这种可能性。再次感谢!:)克里斯汀:不客气!很高兴我们能够帮助您解决此问题。:)@ristenk1如果您好奇,文本字段之前没有重置的原因如下。您为计时器事件添加了一个事件侦听器,该事件在计时器每次滴答声时触发(在您的示例中,每1000毫秒触发一次)。调用stop/reset时,计时器不会发送事件来告诉您它已停止,也不会再次触发事件处理程序。因此,正如Anil建议的,您应该手动处理计时器已重置的事实。