Cocos2d-x和时间有关的代码
用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 + 1900;int month = tm->tm_mon + 1;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 < 0) { 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(); //这是关键2this->addChild(dialogLayer,500,500);}
点击恢复按钮:
}else if(m_pDialogSpecies == DIALOG_GAME_FORGROUND) {timeFlag = 1;//这是关键3,此tag用于图片切换timeSprite = CCSprite::create(RESUME_FIR);CCSize winSize = CCDirector::sharedDirector()->getWinSize();timeSprite->setPosition(ccp(winSize.width/2,winSize.height/2));this->addChild(timeSprite);if (timeSprite) {this->schedule(schedule_selector(FDDialogLayer::updateTime),1);//这是关键4 调度 } }void FDDialogLayer::updateTime(float dt) { if (timeFlag < 3) { timeFlag++; char temp[32]; sprintf(temp, "resume/t%d.png",timeFlag); CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(temp);CCSize conSize = texture->getContentSize();timeSprite->setTexture(texture);timeSprite->setTextureRect(CCRectMake(0, 0, conSize.width, conSize.height));}else{continueGame();//这是关键5 返回场景this->unschedule(schedule_selector(FDDialogLayer::updateTime));} }void FDDialogLayer::continueGame(){MainGameScene *mainGame = (MainGameScene*)CCDirector::sharedDirector()->getRunningScene()->getChildren()->objectAtIndex(0);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); }
转载于:https://www.cnblogs.com/DswCnblog/p/3861321.html
Cocos2d-x和时间有关的代码相关推荐
- 千万不要相信程序员在加班时间写的代码!
其中最重要的就是这条:不要相信一个程序员在加班时间写出来的代码. (软件工程的学说表明,连正常时间好好写的代码,也不要太相信.不过这不是本文的重点,略过不提.) (不懂代码的人,看到本文中的Java代 ...
- asp php时间格式,ASP_asp格式化日期时间格式的代码,' ====================================== - phpStudy...
asp格式化日期时间格式的代码 ' ============================================ ' 格式化时间(显示) ' 参数:n_Flag ' 1:"yyy ...
- 一名优秀的开发人员,空闲时间会敲代码吗
原网页:Do I Need to Code in My Free Time to Be a Good Developer? 作者:Maxim Chechenev "只有在空闲时间也敲代码,才 ...
- 不要相信程序员在加班时间写的代码
http://www.cocoachina.com/programmer/20161215/18359.html 不要相信程序员在加班时间写的代码 2016-12-15 00:01 编辑: suili ...
- python计算时间差代码_Python3显示当前时间、计算时间差及时间加减法示例代码...
Python3显示当前时间.计算时间差及时间加减法示例代码 摘要 在使用Python写程序时,经常需要输出系统的当前时间以及计算两个时间之间的差值,或者将当前时间加减一定时间(天数.小时.分钟.秒)来 ...
- 华为云Astro的前世今生:用7年时间革新低代码开发观念
摘要:深扒华为云Astro低代码平台的前世今生,其成功之路显然是一条"个性"之路. 本文分享自华为云社区<华为云Astro的前世今生:用7年时间革新低代码开发观念> ...
- Java时间类型转换详细代码示例
Java时间类型转换详细代码示例 一.时间类型转换代码示例 二.时间标准简介 1.UTC(世界标准时间) 2.GMT(格林尼治平时) 3.CST(北京时间) 三.SimpleDateFormat 参数 ...
- 水平时间轴css代码_使用CSS和JavaScript构建水平时间线
水平时间轴css代码 在上一篇文章中 ,我向您展示了如何从头开始构建响应式垂直时间轴. 今天,我将介绍创建相关的水平时间线的过程. 与往常一样,要初步了解我们将要构建的内容,请查看相关的CodePen ...
- java开发有不加班的吗_千万不要相信程序员在加班时间写的代码!
作为一个最底层的程序员,我先记录一些只有底层程序员才会知道的事情.如果多年后,我违背自己进入这个行业的初心,走上管理岗位,也能回想起一些禁忌,避免一些错误. 其中最重要的就是这条:不要相信一个程序员在 ...
- (转载)不要相信程序员在加班时间写的代码
作为一个最底层的程序员,我先记录一些只有底层程序员才会知道的事情.如果多年后,我违背自己进入这个行业的初心,走上管理岗位,也能回想起一些禁忌,避免一些错误. 其中最重要的就是这条:不要相信一个程序员在 ...
最新文章
- 单链表按顺序插入节点
- MQTT client id重复导致连接失败
- 【算法与数据结构】最大子序列和问题
- 8张图带你轻松温习Java知识
- 解决阿里云OSS跨域问题
- ACM 博弈专题(5种模板)
- hdu 1286找新朋友 (简单数学题)
- LoadRunner10自带的WEBTOURS,无法显示Flights页面问题解决办法
- 正态分布的前世今生:最小二乘法
- win10自动更新系统导致 点击睡眠之后直接关机
- arcgis中python计算面积的表达式_ArcGIS字段计算中的Python语法错误
- Blazor The specified deps.json \bin\Debug\net5.0\BlazorWebApp.deps.json] does not exist
- 黑产以及一般业务安全的应对思路
- 使用PyTorch实现手写文字识别的学习
- 红米k50至尊版和小米12spro区别 哪个好
- 计算机网络复习-典型题目答案
- 那些年啊,那些事——一个程序员的奋斗史 ——39
- 怎样在表格中选出同一类_如何让excel表格中的几列数据相同的名称数据排列
- 23电工杯数学建模A题
- 一节双曲型方程基于MATLAB的求解,双曲方程基于matlab的数值解法