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

試す

 timer.currentCount

それ以外の

 flash.utils.getTimer()

タイマーが TIMER イベントを発生させた回数を返します。

于 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 に答える