动作脚本 - 创建一个计数器10秒钟

时间:2014-07-24 13:33:45

标签: actionscript-3

我有一个从父母调用的函数,包含影片剪辑的函数被导入,当我调用该函数时,我的计数器似乎永远不会工作,我在几小时内显示这个计数器:分钟:秒这是代码:

var seconds:int = 10000;


function startTimeLeftCounter():void{
trace("start");
txt_timeLeftText.visible = true;
txt_timeLeftCounter.visible = true;
seconds = seconds - 1000;
trace(seconds);
txt_timeLeftCounter.text = (Math.floor(seconds/60)) + ":" + (seconds % 60 >= 10 ? "": "0") + ":" +  (seconds % 60);
if(seconds > 0){ 
    setTimeout(startTimeLeftCounter, 1000);
}else{
    txt_timeLeftText.visible = false;
    txt_timeLeftCounter.visible = false;
    trace("finish");

}
}

我马上得到的跟踪输出是(跟踪(秒)):

start
-1000
finish

1 个答案:

答案 0 :(得分:1)

您只看到此痕迹的原因很简单:"秒" = 0或未定义。 我没有看到你调用函数startTimeLeftCounter()的所有代码和位置,但我可以假设问题是变量"秒"在另一个范围内或通过gc清理。

我测试了你的本地代码:

    private function testTimer():void {
        var seconds:int = 10000;

        function startTimeLeftCounter():void {
            trace("start");

            seconds = seconds - 1000;
            trace(seconds);

            if(seconds > 0) {
                setTimeout(startTimeLeftCounter, 1000);
            } else {
                trace("finish");

            }
        }

        startTimeLeftCounter();
    }

有下一个痕迹:

[trace] start
[trace] 9000
[trace] start
[trace] 8000
...
[trace] start
[trace] 1000
[trace] start
[trace] 0
[trace] finish