动作脚本3.定时器开始倒计时,当我在菜单中,但比赛还没开始我在、还没、定时器、倒计时

2023-09-08 11:50:02 作者:独活

我创建Flash游戏,我得到了奇怪的问题。计时器开始倒计时,当我在菜单中,但比赛还没开始。在菜单中我有按钮,播放,它被点击之后,它增加计时器,但它显示了多久,程序运行(开始从当前时间算)。

I'm creating flash game and I get strange problem. Timer starts count when I'm in menu, but game not started. In menu I have button "Play", after It is clicked It add timer, but It shows how long program is running (start count from current time).

这是主要功能,启动

        public function MemoryGame()
        {
            startMemoryGame.addEventListener(MouseEvent.CLICK, startPlay);
}

这是按钮开始游戏:

function startPlay(e:MouseEvent):void
{
    startMemoryGame();

}

这里是我的功能,其中定时器和其他对象添加。

And here Is my function where Timer and other objects added.

    function startMemoryGame():void
            {


            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();



            var format:TextFormat = new TextFormat();
                format.font = "Verdana";
                format.color = "#E50041";
                format.size = 22;
            txtTime.border = true;
            txtTime.borderColor = 0xFFFFFF;
                //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.y = 55;     
            txtTime.autoSize = TextFieldAutoSize.CENTER;
            txtTime.defaultTextFormat = format;
            message_txt.autoSize = TextFieldAutoSize.CENTER;
            message_txt.defaultTextFormat = format;

                //here Timer starts
                txtTime.text = showTimePassed(0);
            addChild(txtTime);
            tmpTime = timer.currentCount;
            timer.start();


                _cards = new Array();
                _totalMatches = 18;
                _currentMatches = 0;
                createCards();
            }

            private function tick(e:Event):void {
            txtTime.text = showTimePassed(timer.currentCount - tmpTime);                    


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

什么奇怪的是,如果我删除命令 timer.start()我有同样的问题,当我点击startPlay按钮,它增加了当前定时器(例如: 00:12)只是计时器停止。

What is strange that if I remove command timer.start() I have the same problem, after I click "startPlay" button It adds current timer (for example: 00:12) just timer is stopped.

我试图用 timer.reset(); ,但同样的问题,所以我没有更多的想法,什么是错的。我不明白为什么它开始计时,如果我之前没有使用任何功能。请问你能帮帮我吗?非常感谢你。

I tried to use timer.reset();, but the same problem, so I don't have more ideas what's wrong. And I don't understand why It starts count time if I don't used any functions before. Could you help me, please? Thank you very much.

推荐答案

showTimePassed 你有这样一行:

var time = getTimer() - startTime;

的getTimer() 被传递以毫秒为单位自该计划启动(见DOC)的时间

getTimer() gets the time passed in milliseconds since the program started (see doc)

的startTime 的应该的是毫秒的时间,当你开始游戏。

startTime should be the time in ms when you started the game.

所以,如果不是使用定时器在 startMemoryGame ,只是存储在一个成员变量的getTimer的值() ,并且该变量作为参数传递给 showTimePassed 。或更改 showTimePassed 的code,以满足您的需求。

So instead if using the Timer in startMemoryGame, just store the value of getTimer() in a member variable, and pass that variable as the parameter to showTimePassed. Or change the code of showTimePassed to suit your needs.

只是改变这样的:

private function tick(e:Event):void {
    txtTime.text = showTimePassed(timer.currentCount - tmpTime);                    
}

这样:

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

和这样的:     tmpTime = timer.currentCount;

and this: tmpTime = timer.currentCount;

本     tmpTime =的getTimer();

to this tmpTime = getTimer();

这应该给你正确的时间,如果你不使用 tmpTime 其他地方。否则,只需要声明另一个变量。

It should give you the correct time, if you're not using tmpTime anywhere else. Otherwise just declare another variable.

说明: showTimePassed 只是输出过去了,因为该计划启动的时间,但它减去参数的startTime 从时间。 0传递给你的时间,因为启动程序。当时我的建议更改存储由于执行程序的 tmpTime 当你开始游戏,并把它传递给 showTimePassed

Explanation: showTimePassed just outputs the time passed since the program started, but it subtracts the parameter startTime from that time. Passing 0 will give you the time since starting the program. My proposed changes store the time since execution of the program in tmpTime when you start the game, and passes it to showTimePassed.

所以,启动该程序。的getTime()将给〜0

So you start the program. getTime() will give ~0

五秒钟后你preSS启动按钮。的getTime()将是〜5000,存储在tmpTime

Five seconds later you press the start button. getTime() will be ~5000, stored in tmpTime

showTimePassed叫说1秒后与tmpTime(5000)。的getTimer()给出了6000,减去的startTime给你1000是因为你1秒pressed开始。

showTimePassed is called say 1 second later with tmpTime (5000). getTimer() gives 6000, subtract startTime gives you 1000 which is one second since you pressed start.

下面是对成员变量的一些信息的方式: https://en.wikipedia.org/wiki / Member_variable

Here is some info on member variables by the way: https://en.wikipedia.org/wiki/Member_variable