Search code examples
ioscocos2d-xcocos2d-x-2.x

How to make a timer keep running even if game enters background in cocos2d-x c++ game in ios


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

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

I have defined the function as follows.

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


Solution

  • 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

    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.