0
votes

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).

This is main function which starts

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

This is button to start game:

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

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.

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.

1

1 Answers

0
votes

In showTimePassed you have this line:

var time = getTimer() - startTime;

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

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

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.

Just changing this:

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

to this:

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

and this: tmpTime = timer.currentCount;

to this tmpTime = getTimer();

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

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.

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

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

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.

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