1

我正在创建应用程序,我需要显示游戏时间 MM:SS 格式。但我不知道为什么计时器不工作它显示 0:00.359(359 毫秒)并且没有改变。问题出在哪里?我找不到它。谢谢你。

    var timer:Timer; //import flash.utils.Timer;
    var txtTime:TextField;
    var tmpTime:Number;  //this will store the time when the game is started    

//your constructor:
public function MemoryGame()
{
    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

    txtTime = new TextField();
    addChild(txtTime);

    tmpTime = flash.utils.getTimer();
    timer.start(); //start the timer
    //....the rest of your code
}

private function tick(e:Event):void {
    txtTime.text = showTimePassed(flash.utils.getTimer() - tmpTime);
}

//this function will format your time like a stopwatch
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 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) ) ); //60 seconds times 1000 miliseocnds gets the minutes
  return minutes + ":" + leadingZeroS + seconds + "." + leadingZeroMS + miliseconds;
}


//in your you-win block of code:
var score = flash.utils.getTimer() - tmpTime; //this store how many milliseconds it took them to complete the game.
4

2 回答 2

2

Try

 timer.currentCount

instead of

 flash.utils.getTimer()

It will return the number of times the timer has fired the TIMER-Event.

于 2013-05-04T07:57:12.480 回答
0

你不需要这样做。

var time = getTimer() - startTime;

在您的代码中, startTime 已经过了时间,原因是

showTimePassed(flash.utils.getTimer() - tmpTime);

或者你可以打电话

showTimePassed();

并将时间计算更改为

var time = getTimer() - tmpTime;
于 2013-05-04T08:23:31.163 回答