0

我正在创建 Flash 游戏,但我遇到了奇怪的问题。当我在菜单中时计时器开始计数,但游戏没有开始。在菜单中,我有“播放”按钮,单击它后添加计时器,但它显示程序运行了多长时间(从当前时间开始计数)。

这是启动的主要功能

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

这是开始游戏的按钮:

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

}

这是我添加 Timer 和其他对象的函数。

    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)只是计时器停止了。

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

4

1 回答 1

0

showTimePassed你有这一行:

var time = getTimer() - startTime;

getTimer()获取自程序启动以来经过的时间(以毫秒为单位)(请参阅 doc)

startTime 应该是你开始游戏时的毫秒数。

因此,如果在 中使用 Timer startMemoryGame,只需将 的值存储getTimer()在成员变量中,然后将该变量作为参数传递给showTimePassed. 或更改代码showTimePassed以满足您的需要。

只是改变这个:

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;

到这个 tmpTime = getTimer();

如果您不在tmpTime其他任何地方使用,它应该为您提供正确的时间。否则只需声明另一个变量。

解释:showTimePassed只输出程序启动后经过的时间,但从startTime那个时间减去参数。传递 0 将为您提供自启动程序以来的时间。我建议的更改将自程序执行以来的时间存储在tmpTime您开始游戏时,并将其传递给showTimePassed.

所以你启动程序。getTime() 将给出 ~0

五秒钟后,您按下开始按钮。getTime() 将是〜5000,存储在 tmpTime

showTimePassed 用 tmpTime (5000) 在 1 秒后调用。getTimer() 给出 6000,减去 startTime 给出 1000,这是自您按下开始以来的一秒。

顺便说一下,这里有一些关于成员变量的信息:https ://en.wikipedia.org/wiki/Member_variable

于 2013-05-08T18:53:39.257 回答