2015-02-11 165 views
0

嗨我發現這個非常有用的代碼爲計時器計數器,但它開始時,我玩文件。我需要的是一種將它變成MouseEvent.CLICK的方法,以便當用戶按下按鈕時啓動,並在用戶按下另一個按鈕時停止。這是否有能力?Ajusting as3代碼觸發鼠標事件

import flash.utils.Timer; 
import flash.events.Event; 
import flash.events.TimerEvent; 
import flash.globalization.DateTimeFormatter; 

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 hours:String = String(time.hours); 
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 
hours = (hours.length != 2) ? '0'+hours : hours; 
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 = hours + ":" + minutes + ":" + seconds+"." + miliseconds; 

} 

回答

0

做最簡單的事情是使用timer.stop()timer.start()。這並不完全準確,因爲調用stop()start()基本上會重新啓動當前的延遲(100ms),但如果這足夠好,那麼它應該可以工作。

另請注意,計時器代碼並非完全準確,因爲Timer事件是基於幀率和腳本執行時間以輕微偏移量進行調度的。對於準確的計時器,您需要輪詢getTimer(),但暫停和恢復會變得稍微複雜一點。