【问题标题】:Action Script 3. Timer starts count when I'm in menu, but game not started动作脚本 3. 当我在菜单中时计时器开始计数,但游戏没有开始
【发布时间】:2013-05-08 18:14:34
【问题描述】:

我正在创建 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();,但同样的问题,所以我没有更多的想法有什么问题。而且我不明白如果我以前不使用任何功能,它为什么会开始计时。请问你能帮帮我吗?非常感谢。

【问题讨论】:

    标签: actionscript-3 flash actionscript timer count


    【解决方案1】:

    showTimePassed 你有这行:

    var time = getTimer() - startTime;
    

    getTimer() 获取自程序启动以来经过的时间(以毫秒为单位)(参见文档)

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

    因此,如果在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 会在 1 秒后调用 tmpTime (5000)。 getTimer() 得到 6000,减去 startTime 得到 1000,这是你按下开始后一秒。

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

    【讨论】:

    • 感谢您的回答。我真的误解了我需要做什么,如果我从 showTimePassed 中删除 getTimer() 它总是显示 0,你的意思是`只需将 getTimer() 的值存储在成员变量中`?
    • 我的意思不是要删除 getTimer,函数 showTimePassed 可以工作,但它是建立在不同的前提下的,即你在 开始播放时以毫秒为单位传递时间 .所以你需要把那个时间存储在某个地方。可能在 tmpTime 或声明一个新变量。我编辑了我的答案以更清楚地说明这一点
    • 感谢您的回答,这对我有用。我现在有一个问题,游戏结束后如何返回时间?我的意思是如何在计时器停止时获取当前时间?在我使用此urlVars.time = timer.currentCount; 之前,但我不知道这是否正确。
    • 在游戏结束事件中存储getTime() - tmpTime 怎么样?我只会将 Timer 类用于时间显示的更新事件。尽管使用 timer.currentCount 也应该可以。只需尝试一些事情,阅读该函数的文档以了解它们的作用并制定出适合您需求的解决方案。这确实不是一项艰巨的任务,尤其是现在您的其余时间显示都正常工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多