原創作品,轉載請標明:http://blog.csdn.net/jackystudio/article/details/12036237
int score;//即時分數
static int highestHistoryScore;//曆史最高分
//在ControlLayer的init加入scorescoreItem=CCLabelBMFont::create("0","font/font.fnt");//scoreItem是CCLabelBMFont*型成員變數,這裡採用了自訂的字型,相關資源在第一篇有給出了scoreItem->setColor(ccc3(143,146,147));scoreItem->setAnchorPoint(ccp(0,0.5));scoreItem->setPosition(ccp(pPauseItem->getPositionX()+normalPause->getContentSize().width/2+5,pPauseItem->getPositionY()));this->addChild(scoreItem);
//更新分數void ControlLayer::updateScore(int score){if (score>=0 && score<=MAX_SCORE)//設定一個最大值20億{CCString* strScore=CCString::createWithFormat("%d",score);//格式化為字串scoreItem->setString(strScore->m_sString.c_str());//從CCString中獲得char*,更新分數}}
//如果life==1,移除enemy3,else if(enemy3->life==1){enemy3->life--;mutiBulletsToDelete->addObject(mutiBullets);enemy3sToDelete->addObject(enemy3);score+=ENEMY3_SCORE;//增加分數,ENMEY3_SCORE是一個宏,值是擊落敵機3獲得的分數this->controlLayer->updateScore(score);//調用ControlLayer的updateScore更新分數}
bool WelcomeLayer::isHaveSaveFile()//判斷存檔是否存在{if(!CCUserDefault::sharedUserDefault()->getBoolForKey("isHaveSaveFileXml"))//通過設定的bool型標誌位判斷,如果不存在{CCUserDefault::sharedUserDefault()->setBoolForKey("isHaveSaveFileXml", true);//寫入bool判斷位CCUserDefault::sharedUserDefault()->setIntegerForKey("HighestScore",0);//寫入初始分數0CCUserDefault::sharedUserDefault()->flush();//設定完一定要調用flush,才能從緩衝寫入ioreturn false;}else{return true;}}void WelcomeLayer::getHighestHistorySorce(){if (isHaveSaveFile())//如果存在存檔{GameOverLayer::highestHistoryScore=CCUserDefault::sharedUserDefault()->getIntegerForKey("HighestScore",0);//讀取曆史最高分}}
//GameOver層中調用if (score>highestHistoryScore){CCUserDefault::sharedUserDefault()->setIntegerForKey("HighestScore",score);//修改存檔highestHistoryScore=score//最高分重新設定...後面還有其他東西}
那怎麼辦?
: