热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

IamanoobtoAS3soexcuseme,please.我是AS3的菜鸟,请原谅我。IhaveatimeronmyappthatIamtrying

I am a noob to AS3 so excuse me, please.

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

I have a timer on my app that I am trying to use as a stop watch, What I would like to do is start it at a specified time not always at 0?

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

I would like the user to enter in a text box a value of 1 to 99 and the timer starts from there entered value.

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

Thanks in advance.

提前致谢。

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 个解决方案

#1


1  

Well you can't change Timer/currentCount but you can store a start count and display startCount + myTimer.currentCount:

那么你不能改变Timer / currentCount但你可以存储一个开始计数并显示startCount + myTimer.currentCount:

var startCount:int = 100; 

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

As for how to determine the startCount from a user text input: you can use parseInt to convert text to an integer. You should also restrict the text input to numbers, and you can enforce a range of 0-99 using Math.min and Math.max:

至于如何从用户文本输入中确定startCount:您可以使用parseInt将文本转换为整数。您还应该将文本输入限制为数字,并且可以使用Math.min和Math.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);
}

推荐阅读
author-avatar
万承裕常明
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有