0

我正在创建 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 ;



}

谢谢你的回答。

4

1 回答 1

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();
于 2013-05-07T22:09:56.260 回答