actionscript-3 - as3中的定时器初始值

标签 actionscript-3 flash

我有一个在 as3 中计数的计时器。我正在尝试模拟从 07:30 到 12:00 计数的时钟。计数功能正在运行。我如何设置初始起始值和停止值?

这是我的代码:

var timer:Timer = new Timer(100, 50);
timer.start();

timer.addEventListener(TimerEvent.TIMER, timerTickHandler);
var timerCount:int = 10;


function timerTickHandler(Event:TimerEvent):void
{
    timerCount += 10000;
    toTimeCode(timerCount);
}

function toTimeCode(milliseconds:int) : void 
{
    //creating a date object using the elapsed milliseconds
    var time:Date = new Date(milliseconds);

    //define minutes/seconds/mseconds
    var minutes:String = String(time.minutes);
    var seconds:String = String(time.seconds);
    var miliseconds:String = String(Math.round(time.milliseconds)/100);

    //add zero if neccecary, for example: 2:3.5 becomes 02:03.5
    minutes = (minutes.length != 2) ? '0'+minutes : minutes;
    seconds = (seconds.length != 2) ? '0'+seconds : seconds;

    //display elapsed time on in a textfield on stage
    timer_txt.text = minutes + ":" + seconds;

}

最佳答案

有几种方法可以做到这一点。我将分享其中 2 个。

  1. 将日期对象初始化为您的开始时间:(在计时器开始计时之前)

    var time:Date = new Date();
    time.setHours(7, 30);
    

    然后将毫秒添加到每个计时器间隔的时间:

    time.time += milliseconds;
    

    如果您出于某种原因需要停止时钟,这将是一个好方法。

  2. 如果您不需要停止时钟(并且想要一个准确的时钟),您可以执行以下操作:

     var time:Date = new Date();
     time.setHours(7,30);
     var offset:Number = flash.utils.getTimer(); //this gets the amount of milliseconds elapsed since the application started;
    

    然后在你的间隔方法中:

     time.time += flash.utils.getTimer() - offset;
    

关于actionscript-3 - as3中的定时器初始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24938122/

相关文章:

apache-flex - 用不确定的微调器替换 Flex 预加载器杆?

.net - 如何在 .net c# 中压缩字符串并在 flash as3 中解压缩?

javascript - jquery.flash 插件在 IE6 中不工作

c++ - 在网络浏览器中运行 C/C++ 代码?

actionscript-3 - Flash Builder 中的 super() 。为什么?

Flash 10.1 AS3 - 将实时效果应用于麦克风 - 卡顿问题

sqlite - PureMVC 中的异步数据库访问层

apache-flex - 弹性 : How to add a tab close button for TabNavigator component

html - 在 Actionscript 3 项目中显示 HTML

actionscript-3 - Flash CS6错误1120 : Access of undefined property (AS3)