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 7719873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:31:26+00:00 2026-06-01T03:31:26+00:00

Im trying to add a timer to my game so that the user knows

  • 0

Im trying to add a timer to my game so that the user knows how long they have spent playing a level. Ive figured out that I can initialize a timer the following way:

bool showTimer = YES;
NSDate startDate;
UILabel timerLabel; // initialized in viewDidLoad

    -(void) showElapsedTime: (NSTimer *) timer {
        if (showTimer) {
            NSTimeInterval timeSinceStart;

            if(!startDate) {
                startDate = [NSDate date]; 
            }

            timeSinceStart = [[NSDate date] timeIntervalSinceDate:startDate];
            NSString *intervalString = [NSString stringWithFormat:@"%.0f",timeSinceStart];
            timerLabel.text = intervalString;

            if(stopTimer) {//base case
                [timer invalidate];
            }
        }
    }

    - (void) startPolling {
        [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(showElapsedTime:) userInfo:nil repeats:YES];
    }

I start the startPolling method in the viewDidLoad. When I run the app, I do see the timer and it tracks the time but when I exit the app and re-enter it, the timer doesnt pause. I’m also not sure how to handle going to another view (like the options menu) and then coming back to this view. I understand NSDefaults and NSCoding and I see how I could save the current value on the timer as a Coding object, keeping a seperate key-value pair in a plist for every level but this seems cumbersome.

Is there a better way to keep track of how long the user spends in a level?

  • 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-06-01T03:31:27+00:00Added an answer on June 1, 2026 at 3:31 am

    Instead of doing the calculation (subtracting the start time from the current time) every time, since all you care about is an elapsed time, just have a variable like NSTimeInterval elapsedTime that you start at 0 and add time to every time that the timer fires. (If you want to track it to 0.1 seconds like in your example, just divide by 10 before displaying it.) This way, you can pause it whenever you want and it will just continue on from where it was before when it starts up again.

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

Sidebar

Related Questions

I have already this function I'm trying to add a timer like this: when
I'm trying to add a feature into my app that allows the user to
I have built a simple game in WP7 and I am trying to add
I am trying to add and remove events from a timer and I have
I am trying to add the current date (not time if i have the
I'm trying to build a level editor for a game I'm working on. It
I am trying to add a count up timer to my code. My current
I'm trying to add networking over the internet to my game, using Peer to
I'm trying to make a little game that will first show the player a
I just started a game using cocos2d-iphone and I am trying to add a

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.