首先添加计分要使用到的几个变量
int score; //计分变量 CCLabelTTF* labelScore; //显示计分 CCLabelTTF* labelScoreTitle; //显示计分标题
再声明一个计分函数
//计分函数 void doScoring(int number);
在init方法中初始化变量,添加如下代码
score = 0; labelScore = CCLabelTTF::create("0",ScreenAdapter::GetFontName(),40); labelScoreTitle = CCLabelTTF::create("Score",ScreenAdapter::GetFontName(),40); labelScore->setPosition(ccp(visibleSize.width/2,visibleSize.height-50)); labelScoreTitle->setPosition(ccp(visibleSize.width-330,visibleSize.height-50)); addChild(labelScore); addChild(labelScoreTitle);
实现doScoring函数
void GameLayer::doScoring(int number) { score += number; labelScore->setString(ScreenAdapter::GetString(score)); }
在活动逻辑中调用计分函数doScoring
bool GameLayer::doUp() { CCLog("doUp"); bool isdo = false; for (int x = 0; x < 4; x++) { for (int y = 3; y >= 0; y--) { for (int y1 = y - 1; y1 >= 0; y1--) { if (cardArr[x][y1]->getNumber() > 0) { if (cardArr[x][y]->getNumber() <= 0) { cardArr[x][y]->setNumber(cardArr[x][y1]->getNumber()); cardArr[x][y1]->setNumber(0); //移动卡片不计分 y++; isdo = true; }else if(cardArr[x][y]->getNumber() == cardArr[x][y1]->getNumber()){ cardArr[x][y]->setNumber(cardArr[x][y]->getNumber()*2); cardArr[x][y1]->setNumber(0); //有合并卡片操作需要计分 doScoring(cardArr[x][y]->getNumber()); isdo = true; } break; } } } } return isdo; }
其他滑动方向都需要调用,最后来看看运行效果: