用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. Fiddler 扩展编程——oSession相关方法

     // 修改session中的显示样式  oSession["ui-color"] = "orange";  // 移除http头部中的MQB-X5-Refer ...

  2. PHPCMS V9 加密规则

    PHPCMS V9 加密规则 相关表:v9_admin 加密方式: md5(md5(password)+encrypt) 第一步:对输入的密码32位小写   MD5 对输入的密码进行trim过滤 第二 ...

  3. Linux高级权限管理

    传统的UGO(rwx-wx-wx)权限模型,无法解决当多个组需要对一个文件执行某些权限的问题. ACL :访问控制列表access control list一种高级的权限机制,允许我们对文件或者文件夹 ...

  4. 关于python打包成exe的一点经验之谈

    我经常用python写些脚本什么的,有时候脚本写完以后,每次运行都得在IDE打开在运行,很麻烦,所以经常将python编译成exe.SO...有了一点经验,在这和大家分享一下.      python ...

  5. 【Linux】gvim封装至gvi命令

    方法1:使用脚本 #!/bin/bash - #============================================================================ ...

  6. 关于html中的doctype的重要性的认知以及IE的浏览器模式与文档模式

    浏览器模式”用于切换IE针对该网页的默认文档模式.对不同版本浏览器的条件备注解析.发送给网站服务器的用户代理(User-Agent)字符串的值.网站可以根据浏览器返回的不同用户代理字符串判断浏览器的版 ...

  7. 商家 APP 如何接入新版支付宝支付,老版本商家如何升级

    代码地址如下:http://www.demodashi.com/demo/14006.html 前言 支付宝移动支付2.0版本对比1.0版本做了较大更新,新申请的商家都需要采用最新2.0版本 SDK ...

  8. js的7个技巧

    http://www.vaikan.com/seven-javascript-things-i-wish-i-knew-much-earlier-in-my-career/

  9. xargs详解

    一.场景 这个命令是错误的 find ./ -perm +700 |ls -l 这样才是正确的 find ./ -perm +700 |xargs ls -l  二.用法 [root@localhos ...

  10. IDEA创建maven项目jar更新缓慢问题

    转自:http://www.cnblogs.com/ae6623/p/4416256.html 国内连接maven官方的仓库更新依赖库,网速一般很慢,收集一些国内快速的maven仓库镜像以备用. 最新 ...