这个一个大鱼吃小鱼的游戏Demo,我们来按照游戏来解说代码。
详细代码:
HelloWorldScene.h
class HelloWorld : public cocos2d::CCLayer { public: // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // there's no 'id' in cpp, so we recommand to return the exactly class pointer static cocos2d::CCScene* scene(); //a selector callback //NewGame 回调函数 virtual void menuNewGameCallback(CCObject* pSender); // a selector callback //Option 回调函数 virtual void menuOptionCallback(CCObject* pSender); //About 回调函数 virtual void menuAboutCallback(CCObject* pSender); // a selector callback //Quit 回调函数 void menuCloseCallback(CCObject* pSender); // implement the "static node()" method manually CREATE_FUNC(HelloWorld); };
HelloWorldScene.cpp
CCScene* HelloWorld::scene() { CCScene * scene = NULL; do { // 'scene' is an autorelease object scene = CCScene::create(); CC_BREAK_IF(! scene); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); CC_BREAK_IF(! layer); // add layer as a child to scene scene->addChild(layer); } while (0); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { bool bRet = false; do { ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! CCLayer::init()); ////////////////////////////////////////////////////////////////////////// // add your codes below... ////////////////////////////////////////////////////////////////////////// std::string titleStr = "Angry Fish"; //CCLabelTTF* pLable = CCLabelTTF::labelWithString(titleStr.c_str(),"Thonburi",30); CCLabelTTF* pLable = CCLabelTTF::create(titleStr.c_str(),"Thonburi",30); CC_BREAK_IF(!pLable); pLable->setColor(ccRED); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); pLable->setPosition(ccp(winSize.width/2,winSize.height*0.8f)); this->addChild(pLable,1); //CCSprite* pSprite = CCSprite::spriteWithFile("bg01.jpg"); CCSprite* pSprite = CCSprite::spriteWithFile("bg01.png"); CC_BREAK_IF(!pSprite); //pSprite->setScaleX(winSize.width/pSprite->getContentSizeInPixels().width); pSprite->setScaleX(winSize.width/pSprite->getContentSize().width); //pSprite->setScaleY(winSize.height/pSprite->getContentSizeInPixels().height); pSprite->setScaleY(winSize.height/pSprite->getContentSize().height); pSprite->setPosition(ccp(winSize.width/2,winSize.height/2)); this->addChild(pSprite,0); //游戏目录 CCMenuItemFont::setFontName("Thonburi"); CCMenuItemFont::setFontSize(25); std::string menuItemStr="NewGame"; //CCMenuItemFont* newGame = CCMenuItemFont::itemFromString(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuNewGameCallback)); CCMenuItemFont* newGame = CCMenuItemFont::create(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuNewGameCallback)); menuItemStr = "Option"; //CCMenuItemFont* option = CCMenuItemFont::itemFromString(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuOptionCallback)); CCMenuItemFont* option = CCMenuItemFont::create(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuOptionCallback)); menuItemStr = "About"; //CCMenuItemFont* about = CCMenuItemFont::itemFromString(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuAboutCallback)); CCMenuItemFont* about = CCMenuItemFont::create(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuAboutCallback)); menuItemStr = "Quit"; //CCMenuItemFont* quit = CCMenuItemFont::itemFromString(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuCloseCallback)); CCMenuItemFont* quit = CCMenuItemFont::create(menuItemStr.c_str(),this,menu_selector(HelloWorld::menuCloseCallback)); CCMenu* menu = CCMenu::menuWithItems(newGame,option,about,quit,NULL); menu->setPosition(ccp(winSize.width/2,winSize.height/2)); menu->alignItemsVertically(); addChild(menu,1); bRet = true; } while (0); return bRet; } void HelloWorld::menuCloseCallback(CCObject* pSender) { // "close" menu item clicked CCDirector::sharedDirector()->end(); } void HelloWorld::menuOptionCallback(CCObject* pSender) { CCScene* scene = SettingScene::scene(); CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::transitionWithDuration(1.2f,scene)); } void HelloWorld::menuNewGameCallback(CCObject* pSender) { CCScene* scene = GameScene::scene(); CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::transitionWithDuration(1.2f,scene)); } void HelloWorld::menuAboutCallback(CCObject* pSender) { //CCScene* scene = AboutScene::scene(); CCScene* scene = AboutScene::scene(); CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::transitionWithDuration(1.2f,scene)); }
详细代码:
AboutScene.h
class AboutScene : public cocos2d::CCLayer { public: virtual bool init(); static cocos2d::CCScene* scene(); void backCallback(cocos2d::CCObject* pSender); // implement the "static node()" method manually CREATE_FUNC(AboutScene); };
AboutScene.cpp
cocos2d::CCScene* AboutScene::scene() { CCScene * scene = NULL; do { // 'scene' is an autorelease object scene = CCScene::create(); CC_BREAK_IF(! scene); // 'layer' is an autorelease object AboutScene* layer = AboutScene::create(); CC_BREAK_IF(! layer); // add layer as a child to scene scene->addChild(layer); } while (0); // return the scene return scene; } bool AboutScene::init() { bool bRet = false; do { //init background CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCSprite* pSprite = CCSprite::spriteWithFile("bg01.png"); pSprite->setScaleX(winSize.width/pSprite->getContentSize().width); pSprite->setScaleY(winSize.height/pSprite->getContentSize().height); pSprite->setPosition(ccp(winSize.width/2,winSize.height/2)); this->addChild(pSprite,0); std::string titleStr = "Angry Fish\n\n Write by Cocos2d-x\n\n Author:freeman"; CCLabelTTF* pLable = CCLabelTTF::labelWithString(titleStr.c_str(),"Thonburi",30); CC_BREAK_IF(!pLable); pLable->setColor(ccBLUE); //pLable->setAnchorPoint(ccp(0,1)); pLable->setPosition(ccp(winSize.width/2,winSize.height/2)); this->addChild(pLable,1); std::string str; str="Back"; //CCMenuItemFont* backMenu = CCMenuItemFont::itemFromString(str.c_str(),this,menu_selector(AboutScene::backCallback)); CCMenuItemFont* backMenu = CCMenuItemFont::create(str.c_str(),this,menu_selector(AboutScene::backCallback)); backMenu->setAnchorPoint(ccp(1,0)); backMenu->setPosition(ccp(winSize.width,0)); CCMenu* mn = CCMenu::menuWithItems(backMenu,NULL); mn->setPosition(ccp(0,0)); this->addChild(mn,1); bRet = true; } while (0); return(bRet); } void AboutScene::backCallback(cocos2d::CCObject* pSender) { CCScene * scene=HelloWorld::scene(); CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::transitionWithDuration(1.2f,scene)); }
详细代码:
global.h
extern bool g_isMusic; extern bool g_isSound;
global.cpp
bool g_isSound = true; bool g_isMusic = true;
class SettingScene :public cocos2d::CCLayer { public: virtual bool init(); static cocos2d::CCScene* scene(); void backCallback(cocos2d::CCObject* pSender); void soundCallback( cocos2d::CCObject *pSender ); void musicCallback( cocos2d::CCObject *pSender ); //LAYER_NODE_FUNC(SettingScene); CREATE_FUNC(SettingScene); };
SettingScene.cpp
cocos2d::CCScene* SettingScene::scene() { CCScene * scene = NULL; do { // 'scene' is an autorelease object scene = CCScene::create(); CC_BREAK_IF(! scene); // 'layer' is an autorelease object SettingScene *layer = SettingScene::create(); CC_BREAK_IF(! layer); // add layer as a child to scene scene->addChild(layer); } while (0); // return the scene return scene; } bool SettingScene::init() { bool bRet = false; do { CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCSprite* pSprite = CCSprite::spriteWithFile("bg01.png"); pSprite->setScaleX(winSize.width/pSprite->getContentSize().width); pSprite->setScaleY(winSize.height/pSprite->getContentSize().height); pSprite->setPosition(ccp(winSize.width/2,winSize.height/2)); this->addChild(pSprite,0); CCMenuItemFont::setFontName("Thonburi"); CCMenuItemFont::setFontSize(30); std::string str; str = "Music"; //CCMenuItemFont *music = CCMenuItemFont::itemFromString(str.c_str(),NULL,NULL); CCMenuItemFont *music = CCMenuItemFont::create(str.c_str(),NULL,NULL); //music->setIsEnabled(false); music->setEnabled(false); str = "Open"; //CCMenuItemFont *openMusic = CCMenuItemFont::itemFromString(str.c_str()); CCMenuItemFont *openMusic = CCMenuItemFont::create(str.c_str()); str = "Close"; //CCMenuItemFont *closeMusic = CCMenuItemFont::itemFromString(str.c_str()); CCMenuItemFont *closeMusic = CCMenuItemFont::create(str.c_str()); CCMenuItemToggle *musicItem = CCMenuItemToggle::itemWithTarget(this,menu_selector(SettingScene::musicCallback),openMusic,closeMusic,NULL); musicItem->setSelectedIndex(g_isMusic?0:1); str = "Sound"; //CCMenuItemFont *sound = CCMenuItemFont::itemFromString(str.c_str(),NULL,NULL); CCMenuItemFont *sound = CCMenuItemFont::create(str.c_str(),NULL,NULL); //sound->setIsEnabled(false); sound->setEnabled(false); str = "Open"; //CCMenuItemFont *openSound = CCMenuItemFont::itemFromString(str.c_str()); CCMenuItemFont *openSound = CCMenuItemFont::create(str.c_str()); str = "Close"; //CCMenuItemFont *closeSound = CCMenuItemFont::itemFromString(str.c_str()); CCMenuItemFont *closeSound = CCMenuItemFont::create(str.c_str()); CCMenuItemToggle *soundItem = CCMenuItemToggle::itemWithTarget(this,menu_selector(SettingScene::soundCallback),openSound,closeSound,NULL); soundItem->setSelectedIndex(g_isSound?0:1); str="Back"; //CCMenuItemFont* back = CCMenuItemFont::itemFromString(str.c_str(),this,menu_selector(SettingScene::backCallback)); CCMenuItemFont* back = CCMenuItemFont::create(str.c_str(),this,menu_selector(SettingScene::backCallback)); CCMenu* menu = CCMenu::menuWithItems(music,musicItem,sound,soundItem,back,NULL); this->addChild(menu); menu->alignItemsInColumns(2,2,1); bRet = true; } while (0); return(bRet); } void SettingScene::backCallback(cocos2d::CCObject* pSender) { CCScene* scene = HelloWorld::scene(); CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::transitionWithDuration(1.2f,scene)); } void SettingScene::soundCallback( cocos2d::CCObject *pSender ) { g_isSound=!g_isSound; } void SettingScene::musicCallback( cocos2d::CCObject *pSender ) { g_isMusic=!g_isMusic; }