用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. 如何:在 DHTML 代码和客户端应用程序代码之间实现双向通信

    https://msdn.microsoft.com/zh-cn/library/a0746166 可以使用 WebBrowser 控件向 Windows 窗体客户端应用程序添加现有的动态 HTML ...

  2. UEFI是什么?与BIOS的区别在哪里?UEFI详解!

    前几天在帮同事小何笔记本电脑安装64位 Windows 7 的时候,遇到一个从来没有碰到过的问题,使用光盘安装时,提示:Windows无法安装到这个磁盘.选中的磁盘具有MBR分区表.在EFI系统上,W ...

  3. 浏览器 UA 判断

    export const inBrowser = typeof window !== 'undefined' export const inWeex = typeof WXEnvironment != ...

  4. spring boot更换日志为log4j2

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  5. linux修改 时间 时区

    linux系统修改系统时间与时区 | 浏览:3486 | 更新:2014-06-18 19:36 1 2 3 4 5 6 7 分步阅读 有装过Linux系统的人,可能都会有这样的经历,就是该机器安装w ...

  6. MikroTik RB750r2/RB750gr3 操作记录

    1. 客户端的下载 http://www.mikrotik.com/download  下载 winbox 2. Reset重置密码的正确姿势 http://wiki.mikrotik.com/ind ...

  7. Go 1.5keyword搜索文件夹、文件、文件内容_修复一个小BUG

    package main import ( "bufio" "bytes" "flag" "fmt" "io& ...

  8. 訪问可能没有定义的data (通过static类型flash.net:FileReference引用)

    今天使用Flex实现了图片预览及其上传的功能,在整个开发过程中遇到了"訪问可能没有定义的data (通过static类型flash.net:FileReference引用)"错误, ...

  9. Foundations of Machine Learning: The Margin Explanation for Boosting's Effectiveness

    Foundations of Machine Learning: The Margin Explanation for Boosting's Effectiveness 在这一节,我们要回答的一个问题 ...

  10. JavaScript中逗号运算符

    JavaScript中逗号运算符(,)是顺序执行两个表达式.使用方法: expression1, expression2 其中expression1是任何表达式. expression2是任何表达式. ...