Cocos2d-x和时间有关的代码
生活随笔
收集整理的這篇文章主要介紹了
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和时间有关的代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WordPress 运行流程分析
- 下一篇: 2014-07-23 利用ASP.NET