actionscript 3 - Stop clock on pause button -
i have "clock" counting down time of game (from 9 seconds 0 seconds). have button pauses game, want freeze de clock , when click play, game continuos second frozen. don't have idea how this! can me please?
this.addeventlistener(event.enter_frame, updatetimer); function updatetimer(e:event){ var t:uint = gettimer(); var dt:uint = 9000 - t; var totseconds:uint = math.floor(dt/1000); var minutes:uint = math.floor(totseconds/60); var seconds:uint = math.floor(totseconds%60); var minswithz = minutes < 10 ? "0" + minutes : minutes; var secswithz = seconds < 10 ? "0" + seconds : seconds; var time = minswithz + ":" + secswithz; this.tempo_txt.text = time; } pausa.addeventlistener(mouseevent.mouse_up, menupause); function menupause(e:mouseevent){ ...somegame code... voltar.addeventlistener(mouseevent.mouse_up, play); } function play (e:mouseevent){ ...some game code }
you have 2 choices @ least:
1-andre's answer;
2-my answer:
import flash.events.event; var dt:int = 9000 var diff:number=0; var prevdiff:number=0; var currenttime:number=0; var t:uint=0; var totseconds:int=0; var minutes:int=0; var seconds:int=0; var minswithz; var secswithz; var running:boolean=true; var time; this.addeventlistener(event.enter_frame, updatetimer); function updatetimer(e:event){ if(dt<1){dt=1;menupause(null)} trace(running); totseconds= math.floor(dt/1000); minutes= math.floor(totseconds/60); seconds= math.floor(totseconds%60); minswithz = minutes < 10 ? "0" + minutes : minutes; secswithz = seconds < 10 ? "0" + seconds : seconds; time = minswithz + ":" + secswithz; this.tempo_txt.text = time; if(running){ t= gettimer()-diff-prevdiff; dt=5000- t; }else{ diff=gettimer()-currenttime; } } pausa.addeventlistener(mouseevent.mouse_up, menupause); voltar.addeventlistener(mouseevent.mouse_up, reset); function menupause(e:event){ running=false; currenttime=gettimer(); prevdiff+=diff; } function menuplay (e:event){ running=true; } function reset(){ running=true; prevdiff+=9000; }
a little explaination on code:
gettimer() returns miliseconds when opened app ,
you can't pause "gettimer()"
but can time game paused , subtract gettimer().
variable "diff" here.
, "currenttime" when paused game.
andre's answer more easy;
answer more accurate.
because gettimer() returns real miliseconds timer not ticking accurate.enter_frame event , setinterval same timer.
that means if want more simple code, use andre's , if want accurate timer, use mine.
and removing eventlistener (3vilguy's answer) not enough because gettimer won't pause !
i hope answer useful.
Comments
Post a Comment