AS3通过用户输入递增计时器

时间:2016-02-25 15:38:02

标签: actionscript-3

我是AS3的菜鸟,请原谅我。

我的应用程序上有一个计时器,我试图用作秒表,我想要做的是在指定的时间启动它并不总是在0?

我希望用户在文本框中输入值1到99,并且计时器从输入的值开始。

提前致谢。

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

var customtime:int;
clock.text = "00:00";
var myTimer:Timer = new Timer(1000,0);
myTimer.addEventListener(TimerEvent.TIMER, stopwatch);

function stopwatch(event:TimerEvent):void
{

    clock.text = convert_time(myTimer.currentCount);

}

start_btn.addEventListener(MouseEvent.CLICK, startClock);
stop_btn.addEventListener(MouseEvent.CLICK, stopClock);
reset_btn.addEventListener(MouseEvent.CLICK, resetClock);
setTime_btn.addEventListener(MouseEvent.CLICK, setCustomTime);

function startClock(evt:MouseEvent):void
{
    myTimer.start();
}

function stopClock(evt:MouseEvent):void
{
    myTimer.stop();
}

function resetClock(evt:MouseEvent):void
{
    myTimer.reset();
    clock.text = "00:00";
}

function setCustomTime(evt:MouseEvent)
{
        var t1:int = int(setTimeTxt.text)*60;
        clock.text = convert_time(t1);
        customtime = t1;
}


function time_update(t1:int):int
{
    var t2:int=t1*60;
    return t2;
}

function convert_time(currentAmountSecs:int):String
{
    var minutes:int = Math.floor(currentAmountSecs / 60);
    var seconds:int = currentAmountSecs % 60;

    var prependString:String = "";
    if ( minutes < 10 )
    {
        prependString = "0";
    }

    var prependStringsec:String = "";
    if ( seconds < 10 )
    {
        prependStringsec = "0";
    }

    return prependString + minutes + ":" + prependStringsec + seconds;
}

1 个答案:

答案 0 :(得分:1)

您无法更改Timer/currentCount,但可以存储开始计数并显示startCount + myTimer.currentCount

var startCount:int = 100; 

clock.text = convert_time(startCount + myTimer.currentCount);

至于如何从用户文本输入中确定startCount:您可以使用parseInt将文本转换为整数。您还应该将文本输入限制为数字,并且可以使用Math.minMath.max强制执行0-99的范围:

// restrict input characters to numbers
input.restrict = "0-9";

// update the `startCount` any time user input changes
input.addEventListener(Event.CHANGE, inputChange);
function inputChange(e:Event):void {

    // parse an integer from the text input
    startCount = parseInt(input.text);

    // limit the range to 0-99
    startCount = Math.max(0, Math.min(startCount, 99));

    // make sure the input text displays the in-range value
    if (input.text != String(startCount)) 
        input.text = String(startCount);
}