在动作脚本3中重启计时器功能resetClock

时间:2016-08-13 20:34:07

标签: actionscript-3 flash timer actionscript-2 countdown

我是动作脚本3的新手,所以我会大开眼界。

我正在为我的工作场所制作一个简单的5分钟记分牌,现在我仍然坚持使重置按钮工作。

我用(start-btn,stop_btn和reset_btn)的实例制作了3个按钮,但我无法弄清楚reset_btn的功能代码。我需要它回到5:00然后停止,这样你就可以按下开始并再次从(5:00或我的情况300)开始计数。

到目前为止,这是我的代码:

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


var timeRemaining:int = 300;

showTime.text = formatTimeRemaining();

var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, onTimer);

timer.stop();


start_btn.addEventListener(MouseEvent.CLICK, startClock);

function startClock(event:MouseEvent):void
{
timer.start();
}


stop_btn.addEventListener(MouseEvent.CLICK, stopClock);

function stopClock(event:MouseEvent):void
{
timer.stop();
}


reset_btn.addEventListener(MouseEvent.CLICK, resetClock);

function resetClock(event:MouseEvent):void
{
timer.reset();
showTime.text = "300";
}

function onTimer( ev:TimerEvent ):void

{

timeRemaining--;
if (timeRemaining < 0)
{
    timeRemaining = 0;
    loseGame();
}
else
{
    showTime.text = formatTimeRemaining();
}
}

function formatTimeRemaining():String
{
    var mins : int = int (timeRemaining / 60);
    var minstr:String = mins < 10 ? "0" + mins:"" + mins;
    var secs:int = timeRemaining % 60;
    var secstr:String = secs < 10 ? "0" + secs:"" + secs;

return minstr+":"+secstr;
}

function loseGame():void
{
timer.stop();
trace("Countdown is finished.");
showTime.text = formatTimeRemaining() + (" Time's Up!");
}

1 个答案:

答案 0 :(得分:0)

function resetClock(event:MouseEvent):void
{
    timer.stop();
    timeRemaining = 300;
    showTime.text = formatTimeRemaining();
}

只需停止计时器,重置timeRemaining变量然后显示它。