即使游戏在cocos2d-x c ++ ios游戏中进入后台,如何让计时器继续运行 [英] How to make a timer keep running even if game enters background in cocos2d-x c++ ios game

查看:60
本文介绍了即使游戏在cocos2d-x c ++ ios游戏中进入后台,如何让计时器继续运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的cocos2d-x游戏(c ++)ios中使用了一个计时器。我正在使用cocos2d-x 2.2版本。

我的时间函数如下

在我的初始化



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工作正常。但即使游戏进入后台状态,我也有这个计时器继续运行。我试过在appdelegate中评论didEnter Background的主体,但没有成功。任何帮助将不胜感激

谢谢



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

推荐答案

没办法!苹果操作系统暂停你,它有利于电池。在后台进入后你只有机会获得一个时间段。



您可以在进入后台的同时存储您的详细状态并重新激活在前景。



以下是着名的 Ray Wenderlich
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


在我的AppDelegate.cpp中,我在applicationDidEnterBackground函数中编写了以下代码。每当应用程序进入后台并将其存储在CCUserdefault密钥中时,我会以秒为单位获取时间值。当应用程序到达前台时,我再次使用本地系统时间并从我存储在密钥中的时间中减去该时间。以下是我的代码



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.


这篇关于即使游戏在cocos2d-x c ++ ios游戏中进入后台,如何让计时器继续运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆