动作脚本3.游戏重启后将计时器恢复为0

时间:2013-05-07 16:00:30

标签: actionscript-3 flash object actionscript timer

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

怎么了?

1 个答案:

答案 0 :(得分:3)

您可以使用

timer.reset();

重置它然后再次启动它:

timer.start();

有关详细信息,请参阅docs