Actionscript 3 动作脚本3。游戏重新启动后将计时器恢复为0
我正在制作flash游戏,这是一个计时器,它计算玩家在当前关卡上玩多长时间。这是“重启”按钮,我需要在点击这个按钮后重启计时器(从0开始计数) 这是我的计时器代码:Actionscript 3 动作脚本3。游戏重新启动后将计时器恢复为0,actionscript-3,flash,object,actionscript,timer,Actionscript 3,Flash,Object,Actionscript,Timer,我正在制作flash游戏,这是一个计时器,它计算玩家在当前关卡上玩多长时间。这是“重启”按钮,我需要在点击这个按钮后重启计时器(从0开始计数) 这是我的计时器代码: public function MemoryGame() { addChild(CardContainer); tryAgain.addEventListener(MouseEvent.CLICK, restartGame);
public function MemoryGame()
{
addChild(CardContainer);
tryAgain.addEventListener(MouseEvent.CLICK, restartGame);
timer = new Timer(1000); //create a new timer that ticks every second.
timer.addEventListener(TimerEvent.TIMER, tick, false, 0, true); //listen for the timer tick
txtTime = new TextField();
addChild(txtTime);
tmpTime = timer.currentCount;
timer.start();
}
private function tick(e:Event):void {
txtTime.text = showTimePassed(timer.currentCount - tmpTime);
var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = 0xFF0000;
format.size = 16;
format.bold = true;
//txtTime.x = 250;
txtTime.width/2;
var stageCenter_x:Number = stage.stageWidth/2;
var stageCenter_y:Number = stage.stageHeight/2;
var textCenter_x:Number = txtTime.width/2;
var textCenter_y:Number = txtTime.height/2;
txtTime.x = stageCenter_x - textCenter_x;
txtTime.autoSize = TextFieldAutoSize.CENTER;
txtTime.defaultTextFormat = format;
}
function showTimePassed(startTime:int):String {
var leadingZeroMS:String = ""; //how many leading 0's to put in front of the miliseconds
var leadingZeroS:String = ""; //how many leading 0's to put in front of the seconds
var leadingZeroM:String = "";
var time = getTimer() - startTime; //this gets the amount of miliseconds elapsed
var miliseconds = (time % 1000); // modulus (%) gives you the remainder after dividing,
if (miliseconds < 10) { //if less than two digits, add a leading 0
leadingZeroMS = "0";
}
var seconds = Math.floor((time / 1000) % 60); //this gets the amount of seconds
if (seconds < 10) { //if seconds are less than two digits, add the leading zero
leadingZeroS = "0";
}
var minutes = Math.floor((time / (60 * 1000) ) );
if (minutes < 10) { //if seconds are less than two digits, add the leading zero
leadingZeroM = "0";
}
//60 seconds times 1000 miliseocnds gets the minutes
return leadingZeroM + minutes + ":" + leadingZeroS + seconds + "" + leadingZeroMS ;
}
当我使用这个-计时器停止并继续计数,但不重置
function restartGame(e:MouseEvent):void
{
timer.reset();
timer.start();
}
怎么了?您可以使用
timer.reset();
要重置它,然后重新启动它,请执行以下操作:
timer.start();
更多信息请参见谢谢您的回答,但当我使用timer.play()时;我得到错误:
1061:调用一个可能未定义的方法,通过静态类型flash.utils:Timer.
和Timer.reset()的引用播放;只是停止计时器,而不是刷新到“0”。对不起,这是指timer.start()。并将计数器复位为零;不知道为什么这对你不起作用谢谢但这不起作用。timer.reset();我不知道为什么,但工作一样停止,它不重置计时器,只是停止。当我使用timer.start()时;这是一个小测试程序。您将看到它将count重置为零:var timer:timer=new timer(100,5);timer.addEventListener(TimerEvent.timer,timerHandler);timer.start();函数timerHandler(事件:TimerEvent):void{trace(timer.currentCount);if(timer.currentCount==3){timer.reset();timer.start();}}我用代码更新了我的问题,它现在看起来如何。也许这行是错误的'timer.addEventListener(TimerEvent.timer,resetTimer);'?我不知道如何正确使用resetTimer功能。如果我使用resetTimer();我得到错误:1136:参数数量不正确。预期为1。
timer.start();