我正在创建Flash游戏,这里有计时器,显示玩家在当前级别玩的时间。问题是,当游戏在这里开始时没有计时器,它只在1秒后出现,然后它显示00:01秒。我需要在比赛开始时立即出现计时器并显示00:00。
这是我的主要功能。
public function MemoryGame()
{
addChild(CardContainer);
tryAgain.addEventListener(MouseEvent.CLICK, darKarta);
timer = new Timer(1000); //create a new timer that ticks every second.
timer.addEventListener(TimerEvent.TIMER, tick, false, 0, true); //listen for the timer tick
timer.addEventListener(TimerEvent.TIMER, resetTimer);
txtTime = new TextField();
addChild(txtTime);
tmpTime = timer.currentCount;
timer.start();
_cards = new Array();
_totalMatches = 18;
_currentMatches = 0;
createCards();
}
这是我的计时器:
private function tick(e:Event):void {
txtTime.text = showTimePassed(timer.currentCount - tmpTime);
}
function showTimePassed(startTime:int):String {
var leadingZeroMS:String = ""; //how many leading 0's to put in front of the miliseconds
var leadingZeroS:String = ""; //how many leading 0's to put in front of the seconds
var leadingZeroM:String = "";
var time = getTimer() - startTime; //this gets the amount of miliseconds elapsed
var miliseconds = (time % 1000); // modulus (%) gives you the remainder after dividing,
if (miliseconds < 10) { //if less than two digits, add a leading 0
leadingZeroMS = "0";
}
var seconds = Math.floor((time / 1000) % 60); //this gets the amount of seconds
if (seconds < 10) { //if seconds are less than two digits, add the leading zero
leadingZeroS = "0";
}
var minutes = Math.floor((time / (60 * 1000) ) );
if (minutes < 10) { //if seconds are less than two digits, add the leading zero
leadingZeroM = "0";
}
//60 seconds times 1000 miliseocnds gets the minutes
return leadingZeroM + minutes + ":" + leadingZeroS + seconds + "" + leadingZeroMS ;
}
感谢您的回答。
答案 0 :(得分:1)
您的TextField
未初始化,只有在Timer
触发时才会更新。计时器第一次触发是1秒,因此TextField
中出现的第一个值是相同的。
如果使用起始值初始化TextField,则代码可能正常工作:
txtTime = new TextField();
addChild(txtTime);
// set the start time here w/whatever is appropriate
textTime.text = showTimePassed(0);
tmpTime = timer.currentCount;
timer.start();