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.