Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6023293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:58:59+00:00 2026-05-23T03:58:59+00:00

I’m working on a mobile game and need the game clock to pause when

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:59:00+00:00Added an answer on May 23, 2026 at 3:59 am

    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 using getTimer() in the onActivate() and onDeactivate() functions, then add that difference to gameStartTime), or if you don’t want to change gameStartTime, then you calcuate your time based on delta time. Something like:

    private var m_prevTime:int = 0;
    
    public function startGame():void
    {
        // set the prevTime only when you start the game, otherwise you'll 
        // take into account all the time for flash to get going, your init,
        // opening screens etc
        this.m_prevTime = getTimer();
    }
    
    public function showTime(event:Event)
    {
        // get the difference in time
        var currTime:int = getTimer();
        gameTime += currTime - this.m_prevTime // currTime - prevTime = delta time
        this.m_prevTime = currTime; // hold the current time
    
        // display it
        timeDisplay.text = "Time: "+clockTime(gameTime);
    }
    
    
    public function onActivate(event:Event):void 
    {
        // we're reactivating, so make sure prev time is updated
        this.m_prevTime = getTimer();
        addEventListener(Event.ENTER_FRAME, showTime);
    }
    

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.