游戏检查到结束需要一个菜单让用户选择是关闭游戏还是重新开始游戏,我们首先创建一个菜单层DialogLayer:
//DialogLayer.h #ifndef __DIALOGLAYER_SCENE_H__ #define __DIALOGLAYER_SCENE_H__ #include "cocos2d.h" #include "CardSprite.h" using namespace cocos2d; class DialogLayer : public cocos2d::CCLayer { public: virtual bool init(); static cocos2d::CCScene* scene(); CREATE_FUNC(DialogLayer); private: void DialogLayer::menuReplayCallback(CCObject* pSender); void DialogLayer::menuCloseCallback(CCObject* pSender); }; #endif //DialogLayer.cpp #include "DialogLayer.h" #include "GameLayer.h" #include "cocos2d.h" CCScene* DialogLayer::scene() { // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object DialogLayer *layer = DialogLayer::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } bool DialogLayer::init() { if ( !CCLayer::init() ) { return false; } //菜单场景背景 addChild(cocos2d::CCLayerColor::create( ScreenAdapter::GetGameBGColor() )); // 创建菜单并添加到层中 CCLabelTTF* labelReplay = CCLabelTTF::create("Replay",ScreenAdapter::GetFontName(),20); CCLabelTTF* labelEnd = CCLabelTTF::create("Close",ScreenAdapter::GetFontName(),20); CCMenuItem* itemReplay = CCMenuItemLabel::create(labelReplay,this,menu_selector(DialogLayer::menuReplayCallback)); CCMenuItem* itemClose = CCMenuItemLabel::create(labelEnd,this,menu_selector(DialogLayer::menuCloseCallback)); CCMenu* menu = CCMenu::create(itemReplay,itemClose,NULL); //注意最后这个NULL menu->alignItemsVertically(); //对齐方式 CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); menu->setPosition(ccp(visibleSize.width/2,visibleSize.height/2)); addChild(menu); return true; } void DialogLayer::menuReplayCallback(CCObject* pSender) { //重新进入游戏场景 CCDirector::sharedDirector()->replaceScene(GameLayer::scene()); } void DialogLayer::menuCloseCallback(CCObject* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); #else CCDirector::sharedDirector()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif #endif }
有了菜单层后我们在GameLayer.h中声明检查游戏是否结束的函数
//判断游戏是否还能继续 void doCheckGameOver();
在cpp文件实现如下:
void GameLayer::doCheckGameOver(){ bool isGameOver = true; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { //以点(x,y)为基础检查上下左右是否有相同的数值或0 if (cardArr[x][y]->getNumber() == 0|| (x>0&&(cardArr[x][y]->getNumber() == cardArr[x-1][y]->getNumber()))|| (x<3&&(cardArr[x][y]->getNumber() == cardArr[x+1][y]->getNumber()))|| (y>0&&(cardArr[x][y]->getNumber() == cardArr[x][y-1]->getNumber()))|| (y<3&&(cardArr[x][y]->getNumber() == cardArr[x][y+1]->getNumber()))) { isGameOver = false; } } } //如果游戏结算就切换到菜单场景 if (isGameOver) { CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1,DialogLayer::scene())); } }
最后在滑动后调用该函数
void GameLayer::determineDirection() { bool isdo = false; if(abs(startX-endX)>abs(startY-endY)) //左右滑动 { if(startX > endX) //向右滑动 isdo = doLeft(); else isdo = doRight(); } else //上下滑动 { if(startY < endY) //向上滑动 isdo = doUp(); else isdo = doDown(); } doCheckGameOver(); if(isdo){ autoCreateCardNumber(); } }