Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a timer in my cocos2d-x game (c++) ios. I am using cocos2d-x 2.2 version.
My function for time is as follows
in my init

C++
this->schedule(schedule_selector(HelloWorld::UpdateTimer), 1);


I have defined the function as follows.

C++
void HelloWorld::UpdateTimer(float dt)
{
    if(seconds<=0)
    {
        CCLOG("clock stopped");
        CCString *str=CCString::createWithFormat("%d",seconds);
        timer->setString(str->getCString());
        this->unschedule(schedule_selector(HelloWorld::UpdateTimer));

    }
    else
    {
    CCString *str=CCString::createWithFormat("%d",seconds);
    timer->setString(str->getCString());
    seconds--;
    }

}


Everythings is working fine. But i have this timer to keep running even if the game enters background state. I have tried commenting the body of didEnter Background in appdelegate but not successfull. Any help will be appreciated
Thanks
Posted

No way!!! Apples OS suspends you and it is good for battery sake. You have only the chance to get a time slot after going in the background.

You can store your detailed state in the moment of going in the background and reactivate in going in the foreground.

Here is some enlightment of background by the famous Ray Wenderlich
 
Share this answer
 
In my AppDelegate.cpp I wrote the following code in applicationDidEnterBackground function. Here I took a value of time in seconds whenever the app goes background and store it in a CCUserdefault key. And when the app comes to foreground I again took the local system time and subtracted that from the time I stored in the key. Following is my code

C++
void AppDelegate::applicationDidEnterBackground() 
{
    time_t rawtime;
    struct tm * timeinfo;
    time (&rawtime);
    timeinfo = localtime (&rawtime);

    CCLog("year------->%04d",timeinfo->tm_year+1900);
    CCLog("month------->%02d",timeinfo->tm_mon+1);
    CCLog("day------->%02d",timeinfo->tm_mday);

    CCLog("hour------->%02d",timeinfo->tm_hour);
    CCLog("minutes------->%02d",timeinfo->tm_min);
    CCLog("seconds------->%02d",timeinfo->tm_sec);

    int time_in_seconds=(timeinfo->tm_hour*60)+(timeinfo->tm_min*60)+timeinfo->tm_sec;
    CCLOG("time in seconds is %d",time_in_seconds);
    CCUserDefault *def=CCUserDefault::sharedUserDefault();
    def->setIntegerForKey("time_from_background", time_in_seconds);

    CCDirector::sharedDirector()->stopAnimation();

// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

void AppDelegate::applicationWillEnterForeground() 
{

    CCUserDefault *def=CCUserDefault::sharedUserDefault();
    int time1=def->getIntegerForKey("time_from_background");
    time_t rawtime;
    struct tm * timeinfo;
    time(&rawtime);
    timeinfo = localtime (&rawtime);

    CCLog("year------->%04d",timeinfo->tm_year+1900);
    CCLog("month------->%02d",timeinfo->tm_mon+1);
    CCLog("day------->%02d",timeinfo->tm_mday);

    CCLog("hour------->%02d",timeinfo->tm_hour);
    CCLog("mintus------->%02d",timeinfo->tm_min);
    CCLog("seconds------->%02d",timeinfo->tm_sec);

    int time_in_seconds=(timeinfo->tm_hour*60)+(timeinfo->tm_min*60)+timeinfo->tm_sec;
    int resume_seconds= time_in_seconds-time1;
    CCLOG("app after seconds ==  %d", resume_seconds);
    CCDirector::sharedDirector()->startAnimation();

// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}

You can see and calculate the time the app remained in background.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900