1

我正在创建 Flash 游戏,这是一个计时器,用于计算玩家在当前级别上玩了多长时间。这里是“重新启动”按钮,单击此按钮后我需要重新启动计时器(从 0 开始计数)。

这是我的计时器代码:

 public function MemoryGame()
            {
                addChild(CardContainer);
                tryAgain.addEventListener(MouseEvent.CLICK, restartGame);
                    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 = timer.currentCount;
                    timer.start();
    }

            private function tick(e:Event):void {
               txtTime.text = showTimePassed(timer.currentCount - tmpTime);                 
                var format:TextFormat = new TextFormat();
                format.font = "Verdana";
                format.color = 0xFF0000;
                format.size = 16;
                format.bold = true;  
                //txtTime.x = 250;
                txtTime.width/2;

                var stageCenter_x:Number = stage.stageWidth/2;
                var stageCenter_y:Number = stage.stageHeight/2;

                var textCenter_x:Number = txtTime.width/2;
                var textCenter_y:Number = txtTime.height/2;

                txtTime.x = stageCenter_x - textCenter_x;

                txtTime.autoSize = TextFieldAutoSize.CENTER;
               txtTime.defaultTextFormat = format;

    }
    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 ;



    }

当我使用:removeChild(txtTime);并且稍后addChild(txtTime);计时器不重新启动时,只是删除了时间,稍后再次添加并继续计时。如何将计时器恢复为0?有任何想法吗?非常感谢。

编辑:
使用 restartGame() 函数时,计时器必须重置。

当我使用它时 - 没有任何反应。

public function MemoryGame()
        {
            addChild(CardContainer);
            tryAgain.addEventListener(MouseEvent.CLICK, restartGame);
                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();
}

        function restartGame(e:MouseEvent):void
    {
        timer.addEventListener(TimerEvent.TIMER, resetTimer);
    }

        function resetTimer(event:TimerEvent):void{
            timer.reset();
            timer.start();
        }

当我使用它时 - 计时器停止并继续计数,但不重置。

        function restartGame(e:MouseEvent):void
    {
        timer.reset();
        timer.start();
    }

怎么了?

4

1 回答 1

3

您可以使用

timer.reset();

重置它,然后重新启动它:

timer.start();

有关更多信息,请参阅文档

于 2013-05-07T16:19:57.700 回答