用cocos2d-x获取系统时间,格式为年月日时分秒:

void GetTime(float dt)
{
struct tm *tm;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
//win32平台
time_t timep;
time(&timep);
tm = localtime(&timep);
#else
//android、ios平台 struct cc_timeval now;
CCTime::gettimeofdayCocos2d(&now, NULL);
tm = localtime(&now.tv_sec);
#endif
int year = tm->tm_year + ;
int month = tm->tm_mon + ;
int day = tm->tm_mday;
int hour=tm->tm_hour;
int min=tm->tm_min;
int second=tm->tm_sec;
}

实现游戏倒计时的代码:

在CCDirector中有个getScheduler()得到时刻表【调度者】,我们可以使用时刻表【调度者】CCScheduler,来暂停所有的调度。在CCScheduler暂停所有的调度是这样子的:

CCScheduler.h

/** Pause all selectors from all targets.
You should NEVER call this method, unless you know what you are doing.
//提示我们不应该调用此方法,但是目前我测试中没有出现什么问题
@since v2.0.0
*/
CCSet* pauseAllTargets();

CCSchedule.cpp

CCSet* CCScheduler::pauseAllTargets()
{
return pauseAllTargetsWithMinPriority(kCCPrioritySystem);
} CCSet* CCScheduler::pauseAllTargetsWithMinPriority(int nMinPriority)
{
CCSet* idsWithSelectors = new CCSet();// setWithCapacity:50];
idsWithSelectors->autorelease();
// Custom Selectors
for(tHashSelectorEntry *element = m_pHashForSelectors; element != NULL;
element = (tHashSelectorEntry*)element->hh.next)
{
element->paused = true;
idsWithSelectors->addObject(element->target);
} // Updates selectors
tListEntry *entry, *tmp;
if(nMinPriority < ) { DL_FOREACH_SAFE( m_pUpdatesNegList, entry, tmp ) { if(entry->priority >= nMinPriority)
{
entry->paused = true;
idsWithSelectors->addObject(entry->target);
}
}
} if(nMinPriority) paused = true; idsWithSelectors->addObject(entry->target); } } DL_FOREACH_SAFE( m_pUpdatesPosList, entry, tmp ) { if(entry->priority >= nMinPriority) { entry->paused = true; idsWithSelectors->addObject(entry->target); } } return idsWithSelectors; }

暂停之后,我们再次执行的时候需要恢复,方法如下:

CCScheduler.h

/** Resumes the target.

The 'target' will be unpaused, so all schedule selectors/update will be 'ticked' again.

If the target is not present, nothing happens.

@since v0.99.3

*/

void resumeTarget(CCObject *pTarget);

/** Resume selectors on a set of targets.

This can be useful for undoing a call to pauseAllSelectors.

@since v2.0.0

*/

void resumeTargets(CCSet* targetsToResume);

CCScheduler.cpp

void CCScheduler::resumeTarget(CCObject *pTarget)

{

CCAssert(pTarget != NULL, "");

// custom selectors

tHashSelectorEntry *pElement = NULL;

HASH_FIND_INT(m_pHashForSelectors, &pTarget, pElement);

if (pElement)

{

pElement->paused = false;

}

// update selector

tHashUpdateEntry *pElementUpdate = NULL;

HASH_FIND_INT(m_pHashForUpdates, &pTarget, pElementUpdate);

if (pElementUpdate)

{

CCAssert(pElementUpdate->entry != NULL, "");

pElementUpdate->entry->paused = false;

}

}

void CCScheduler::resumeTargets(CCSet* pTargetsToResume)

{

CCSetIterator iter;

for (iter = pTargetsToResume->begin(); iter != pTargetsToResume->end(); ++iter)

{

resumeTarget(*iter);

}

}

在看完源码之后,来说下实际运用:

点击暂停按钮:

void MainGameScene::pauseMenuCallBack( CCObject *pSender )
{
FDDialogLayer *dialogLayer = FDDialogLayer::create(DIALOG_GAME_PAUSE);
_m_pBeforeTargetSets = CCDirector::sharedDirector()->getScheduler()->pauseAllTargets(); //这是关键1
_m_pBeforeTargetSets->retain(); //这是关键2   this->addChild(dialogLayer,,); }

点击恢复按钮:

}else if(m_pDialogSpecies == DIALOG_GAME_FORGROUND)
{
  timeFlag = ;//这是关键3,此tag用于图片切换
  timeSprite = CCSprite::create(RESUME_FIR);   CCSize winSize = CCDirector::sharedDirector()->getWinSize();
  timeSprite->setPosition(ccp(winSize.width/,winSize.height/));
  this->addChild(timeSprite);   if (timeSprite) {
    this->schedule(schedule_selector(FDDialogLayer::updateTime),);//这是关键4 调度   }
} void FDDialogLayer::updateTime(float dt)
{
  if (timeFlag < )
  {
    timeFlag++;
    char temp[];
    sprintf(temp, "resume/t%d.png",timeFlag);
    CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(temp);     CCSize conSize = texture->getContentSize();
    timeSprite->setTexture(texture);
    timeSprite->setTextureRect(CCRectMake(, , conSize.width, conSize.height));   }else
  {
    continueGame();//这是关键5 返回场景
    this->unschedule(schedule_selector(FDDialogLayer::updateTime));
  }
} void FDDialogLayer::continueGame() {   MainGameScene *mainGame = (MainGameScene*)CCDirector::sharedDirector()->getRunningScene()->getChildren()->objectAtIndex();   CCSet *set = mainGame->getBeforeTargetSets();//这是关键6,这是暂停所有调度者时的返回值   CCDirector::sharedDirector()->getScheduler()->resumeTargets(set);//这是关键7 恢复游戏   this->removeFromParentAndCleanup(true);   CCTextureCache::sharedTextureCache()->removeTextureForKey(RESUME_FIR);
  CCTextureCache::sharedTextureCache()->removeTextureForKey(RESUME_SEC);
  CCTextureCache::sharedTextureCache()->removeTextureForKey(RESUME_TIR);
}
 

Cocos2d-x和时间有关的代码的更多相关文章

  1. 基于jQuery发展历程时间轴特效代码

    分享一款基于jQuery发展历程时间轴特效代码,带左右箭头,数字时间轴选项卡切换特效下载.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div id="time ...

  2. cocos2d 高仿doodle jump 无源代码

    1. 游戏视频 主角眼熟吗?没错,上次跑酷游戏中的"30"来Jump了,有三种道具.主角光环,竹蜻蜓.翅膀: 有两种怪物,螃蟹和鸟: 有5种板子.点击屏幕,30会把它的嘴巴3给发射 ...

  3. 完整显示当前日期和时间的JS代码(2007年2月25日星期日正午12:42:48)

    代码演示效果为“2007年2月25日星期日正午12:42:48”. 使用方法:将下面的JS代码放到你想要显示的页面中(支持HTML页面),然后在你想要显示时间的位置插入下面的代码即可 <div ...

  4. C基础 时间业务实战代码

    引言 业务代码中遇到这样需求, 1. 二者是同一天吗, 2. 时间戳和时间串来回转, 3. 其它扩展需求 等. C写代码同样需要处理这方面时间问题. 本文就是为了解决这个问题. 相比其它时间库, 这里 ...

  5. 预计阅读时间核心jS代码

    <script type="text/javascript"> jq(document).ready(function() { var read_time=jq(&qu ...

  6. Xcode 改时间问题 lua代码没反应问题

    Xcode 改时间问题  rm -fr $_TARGET_BUILD_CONTENTS_PATH/$1/*

  7. Android仿快递 物流时间轴 的代码实现

    首先,这篇参考了别人的代码.根据自己的项目需求简单改造了一下,效果图如下 xml:代码 <?xml version="1.0" encoding="utf-8&qu ...

  8. Android 常用时间格式转换代码

    /** * 获取现在时间 * * @return 返回时间类型 yyyy-MM-dd HH:mm:ss */ public static Date getNowDate() { Date curren ...

  9. 计算文章作品发布时间的php代码

    /* 计算发布时间据当前时间 如1秒前 1分钟前 1小时 1天 1个星期 1个人月 1年 */ function format_dates($time) { if($time <= 0) ret ...

随机推荐

  1. 子查询一(WHERE中的子查询)

    子查询 子查询就是指的在一个完整的查询语句之中,嵌套若干个不同功能的小查询,从而一起完成复杂查询的一种编写形式,为了让读者更加清楚子查询的概念. 子查询返回结果子查询可以返回的数据类型一共分为四种: ...

  2. springboot EnableAutoConfiguration

    http://blog.javachen.com/2016/02/19/spring-boot-auto-configuration.html 自动配置 在启动类上使用@EnableAutoConfi ...

  3. Swift Modules for React Native

    React Native is an Objective-C application framework that bridges JavaScript applications running in ...

  4. Java 爬虫(获取指定页面中所有的邮箱地址)

    import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.io.In ...

  5. npm换国内淘宝镜像

    来源于:http://yijiebuyi.com/blog/b12eac891cdc5f0dff127ae18dc386d4.html 为什么要换源? npm 官方站点 http://www.npmj ...

  6. NET的基本用法(摘)

    摘自:http://baike.baidu.com/link?url=Knc-OicoX8CPcaMS0r3eU8z8ns9z1S6OsRaBTYUIT1raU0FsPWQ35xL-dlxKg9Oy# ...

  7. HDUOJ---汉洛塔IX

    汉诺塔IX Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  8. JS中函数的 this 各种指向

    this是js的一个关键字,随着函数使用场合不同,this的值会发生变化.但是总有一个原则,那就是this指的是调用函数的那个对象. 情形1:如果一个函数中有this,但是它没有被上一级的对象所调用, ...

  9. ios NSURLSession使用说明及后台工作流程分析

    NSURLSession是iOS7中新的网络接口,它与咱们熟悉的NSURLConnection是并列的.在程序在前台时,NSURLSession与NSURLConnection可以互为替代工作.注意, ...

  10. hadoop集群虚拟机配置

    hadoop_1, hadoop_2, hadoop_3 用户名riluo 密码19841984 查看Linux自带的JDK是否已安装 (卸载centOS已安装的1.4) 安装好的CentOS会自带O ...