Actionscript 3 动作脚本3。定时器MM:SS不';行不通

Actionscript 3 动作脚本3。定时器MM:SS不';行不通,actionscript-3,flash,actionscript,time,timer,Actionscript 3,Flash,Actionscript,Time,Timer,我正在创建应用程序,我需要显示游戏时间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 c

我正在创建应用程序,我需要显示游戏时间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.
var定时器:定时器//导入flash.utils.Timer;
var txtTime:TextField;
var-tmpTime:数字//这将存储游戏开始时的时间
//您的构造函数:
公共函数MemoryGame()
{
timer=new timer(1000);//创建一个每秒滴答作响的新计时器。
timer.addEventListener(TimerEvent.timer,tick,false,0,true);//侦听计时器的tick
txtTime=新文本字段();
addChild(txtTime);
tmpTime=flash.utils.getTimer();
timer.start();//启动计时器
//……您的代码的其余部分
}
专用功能勾选(e:事件):无效{
txtTime.text=showTimePassed(flash.utils.getTimer()-tmpTime);
}
//此功能将格式化您的时间,就像秒表一样
函数showTimePassed(startTime:int):字符串{
var leadingZeroMS:String=”“;//在毫秒前放置多少前导0
var leadingZeroS:String=”“;//在秒前放置多少前导0
var-time=getTimer()-startTime;//这将获取经过的毫秒数
var milisseconds=(时间%1000);//模数(%)给出除法后的余数,
如果(毫秒<10){//如果少于两位数,则添加前导0
leadingZeroMS=“0”;
}
var seconds=Math.floor((time/1000)%60);//这是秒数
如果(秒<10){//如果秒小于两位数,则添加前导零
leadingZeroS=“0”;
}
var minutes=Math.floor((time/(60*1000));//60秒乘以1000毫秒得到分钟数
返回分钟数+”:“+前导零+秒数+”+前导零毫秒+毫秒;
}
//在您的you win代码块中:
var score=flash.utils.getTimer()-tmpTime//这个商店记录了他们完成游戏所用的毫秒数。
试试看

而不是

 flash.utils.getTimer()
它将返回计时器触发计时器事件的次数。

您无需执行此操作

var time = getTimer() - startTime;
在代码中,由于

showTimePassed(flash.utils.getTimer() - tmpTime);
或 你可以打电话

showTimePassed();
并将时间计算更改为

var time = getTimer() - tmpTime;

非常感谢。回答正确。我认为你不应该依赖timer.currentCount。计时器不会在准确的时间敲响。事实上,在任何计时器实现中,它们所能保证的只是它在某个特定时间之前不会命中。换句话说,tick()函数在1秒之前不会命中,但通常需要1.035秒(随机增加的额外持续时间)
var time = getTimer() - tmpTime;