I’m working on a mobile game and need the game clock to pause when the game loses focus, for example when call comes in. I have everything working except that when the game gains focus again the time adjusts to as if it had been running the whole time. If someone is on a 3 minute call when they get back they should find the game time where it was.
Here is my code:
public function showTime(event:Event){
gameTime = getTimer()-gameStartTime;
timeDisplay.text = "Time: "+clockTime(gameTime);
}
public function clockTime(ms:int) {
var seconds:int = Math.floor(ms/1000);
var minutes:int = Math.floor(seconds/60);
seconds -= minutes*60;
var timeString:String = minutes+":"+String(seconds+100).substr(1,2);
return timeString;
}
public function onActivate(event:Event):void {
addEventListener(Event.ENTER_FRAME, showTime);
}
public function onDeactivate(event:Event):void {
removeEventListener(Event.ENTER_FRAME, showTime);
}
I’ve been Googling this for two days and am stuck. Could someone please point me in the right direction? Some sample code would be a benefit too since I’m pretty new to AS3. Thanks!
Rich
You’re calculating your time based on the current time –
gameStartTime. You either need to take the delay into account (calculate the total time stopped by usinggetTimer()in theonActivate()andonDeactivate()functions, then add that difference togameStartTime), or if you don’t want to changegameStartTime, then you calcuate your time based on delta time. Something like:You’ll miss out on a frame’s time when coming back from the update, but it’s not noticable (or you can subtract a frame’s time in the
onActivate()function)