actionscript 3 - Action Script 3. Timer starts count when I'm in menu, but game not started -


i'm creating flash game , strange problem. timer starts count when i'm in menu, game not started. in menu have button "play", after clicked add timer, shows how long program running (start count current time).

this main function starts

        public function memorygame()         {             startmemorygame.addeventlistener(mouseevent.click, startplay); } 

this button start game:

function startplay(e:mouseevent):void {     startmemorygame();  } 

and here function timer , other objects added.

    function startmemorygame():void             {               timer = new timer(1000); //create new timer ticks every second.             timer.addeventlistener(timerevent.timer, tick, false, 0, true); //listen 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 put in front of miliseconds       var leadingzeros:string = ""; //how many leading 0's put in front of seconds       var leadingzerom:string = "";        var time = gettimer() - starttime; //this gets amount of miliseconds elapsed       var miliseconds = (time % 1000); // modulus (%) gives remainder after dividing,         if (miliseconds < 10) { //if less 2 digits, add leading 0         leadingzeroms = "0";       }        var seconds = math.floor((time / 1000) % 60); //this gets amount of seconds        if (seconds < 10) { //if seconds less 2 digits, add leading 0         leadingzeros = "0";       }        var minutes = math.floor((time / (60 * 1000) ) );         if (minutes < 10) { //if seconds less 2 digits, add leading 0         leadingzerom = "0";       }       //60 seconds times 1000 miliseocnds gets minutes       return leadingzerom + minutes + ":" + leadingzeros + seconds ; } 

what strange if remove command timer.start() have same problem, after click "startplay" button adds current timer (for example: 00:12) timer stopped.

i tried use timer.reset();, same problem, don't have more ideas what's wrong. , don't understand why starts count time if don't used functions before. me, please? thank much.

in showtimepassed have line:

var time = gettimer() - starttime; 

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

starttime should time in ms when started game.

so instead if using timer in startmemorygame, store value of gettimer() in member variable, , pass variable parameter showtimepassed. or change code of showtimepassed suit 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 tmptime = gettimer();

it should give correct time, if you're not using tmptime anywhere else. otherwise declare variable.

explanation: showtimepassed outputs time passed since program started, subtracts parameter starttime time. passing 0 give time since starting program. proposed changes store time since execution of program in tmptime when start game, , passes showtimepassed.

so start program. gettime() give ~0

five seconds later press start button. gettime() ~5000, stored in tmptime

showtimepassed called 1 second later tmptime (5000). gettimer() gives 6000, subtract starttime gives 1000 1 second since pressed start.

here info on member variables way: https://en.wikipedia.org/wiki/member_variable


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -