用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. 子查询二(在HAVING子句中使用子查询)

    HAVING子句的主要功能是对分组后的数据进行过滤,如果子查询在HAVING中表示要进行分组过滤,一般返回单行单列的数据 示例一.查询部门编号,人数,平均工资,并且要求这些部门的平均工资高于公司的平均 ...

  2. Idea Spring-boot gradle lombok

    1:build.gradle compile("org.projectlombok:lombok:1.16.16") 2:idea安装lombok插件 3:设置 4:重启

  3. jedis操作redis的几种常见方式总结

    Redis是一个著名的key-value存储系统,也是nosql中的最常见的一种,这篇文章主要给大家总结了关于在java中jedis操作redis的几种常见方式,文中给出了详细的示例代码供大家参考学习 ...

  4. 【Qt】splitter

    一段简单的切割窗体的程序: <span style="font-size:18px;">#include "mainwindow.h" #inclu ...

  5. Linux 驱动之内核定时器

    1.定时器 之前说过两类跟时间相关的内核结构. 1.延时:通过忙等待或者睡眠机制实现延时. 2.tasklet和工作队列,通过某种机制使工作推后运行,但不知道运行的详细时间. 接下来要介绍的定时器,可 ...

  6. Javascript获取当月的天数

    var d = new Date(); var curMonthDays = new Date(d.getFullYear(), (d.getMonth() + 1), 0).getDate(); a ...

  7. mysql利用yum安装指定数据存放路径

    测试环境: Centos6.5.MySQL5.6.28 yum安装具有速度快,便捷关键是不用编译,编译时间太久了! 01.下载mysql https://mirrors.tuna.tsinghua.e ...

  8. 普通spring jsp+mybatis项目修改为springboot + jsp +mybatis项目

    概述 由于公司决定使用spring cloud,但是公司积累了大量的普通的jsp项目,老的项目直接全部修改为springboot成本过高,周期比较长,而且公司业务正在快速拓展,所以需要把之前的老项目修 ...

  9. 【转载】delphi下如何复制文件

    1. CopyFile(PChar(源目录),PChar(目标目录),True); CopyFileTo('F:\MyProject\delphi\message\data\data.mdb','c: ...

  10. 记一次400错误引发的血案(URL中特殊符号的转义/400 bad request错误)

    django+nginx+uwsgi部署的站点访问某个URL时发生了400 bad request的错误,而使用django自带的开发版的web server时没有遇到此问题.初步判断是nginx或u ...