2015-05-16 16 views
0

我有以下代碼計數,但我不知道如何能夠包括一個if else語句,例如在15秒時計數停止。AS3如何設置最大時間計數

下面是代碼:

var timer:Timer = new Timer(100); 
    timer.start(); 
    timer.addEventListener(TimerEvent.TIMER, timerTickHandler); 
    var timerCount:int = 0; 

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

    function toTimeCode(milliseconds:int) : void { 
     //create 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+"." + miliseconds; 
    } 

回答

0

首先,爲了提高效率,你可以使用內置的currentCount財產定時器知道多少時間之後(而不是使一個timerCount VAR爲此)

要在15秒後停止計時,只需設置合適的重複次數,因此在15秒時結束,或停止它的節拍處理後15秒過去:

var timer:Timer = new Timer(100,150); //adding the second parameter (repeat count), will make the timer run 150 times, which at 100ms will be 15 seconds. 
timer.start(); 
timer.addEventListener(TimerEvent.TIMER, timerTickHandler); 
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerFinished); //if you want to call a function when all done 

function timerTickHandler(Event:TimerEvent):void{ 
    toTimeCode(timer.delay * timer.currentCount); //the gives you the amount of time past 

    //if you weren't using the TIMER_COMPLETE listener and a repeat count of 150, you can do this: 
    if(timer.delay * timer.currentCount >= 15000){ 
     timer.stop(); 
     //do something now that your timer is done 
    } 
} 
+0

ŧ漢克LDMS!在代碼中有一個錯字:if(timer.dealy應該是:if(timer.delay ... – user2163224

+0

不錯,我會更新它。 – BadFeelingAboutThis

相關問題