1. 程式人生 > >cocos2d x遊戲開發系列教程 中國象棋03 主介面

cocos2d x遊戲開發系列教程 中國象棋03 主介面

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

前情回顧

上個部落格說道我們象棋程式進入了歡迎介面,在歡迎介面下等待一秒進入主介面

進入主介面的關鍵程式碼如下:

CCScene* pScene = CCMainMenu::scene();  建立scene

...

CCDirector::sharedDirector()->replaceScene(ps);  顯示scene


最關鍵的是CCMainMenu::scene函式,說明後面要顯示的scene型別是CCMainMenu型別的

所以接下來我們一起來看CCMainMenu是怎麼回事


CCMainMenu

typedef struct{ CCSprite* pSprite; float x; // coordinate float y;}SPRITE_INFO;typedef std::vector<SPRITE_INFO> VEC_SPRITE;typedef
VEC_SPRITE::iterator VEC_SPRITE_IT;class CCMainMenu : public CCLayer{publicstatic CCScene* scene()virtual bool init(); CREATE_FUNC(CCMainMenu); virtual void ccTouchesEnded(CCSet* pTouches, CCEvent* pEvent);privatevoid  setPos(CCSprite* pSprite, int x, int y)
void  setPosDigit(CCSprite* pSprite, int x, int y)void  menuCallBack(CCObject* pSender)void  menuRegret(CCObject* pSender)void  menuStart(CCObject* pSender)void  menuHardChoose(CCObject* pSender)void  menuSoundChoose(CCObject* pSender)void  updateTime(float dt)void  updateFocus(float dt)void  initCoordinate()void  initListImage()void  initTimeShow()void  initChessPosition();  CCSprite* getNumberSprite(int nNum)void  setNumberSprite(int nChessTime)CCSprite* getListSprite(CHESS_TYPE nType)CHESS_TYPE  getChessType(int i, int j)void  dealWithChess(float x, float y)CCSprite* getChessByCoord(float x, float y)bool  getChessByCoord(float& x, float& y, short dt)void  collectInfo(CCSprite* pSprite)bool  judgeAction(int tx, int ty)void  freeOne()void  freeAll()bool  judgeContinuous()void  clean()bool  judgeWin()//void  print();private: CCSprite* m_pFocus; CCSprite* m_pBKG; CCSprite* m_pMinute[2]; CCSprite* m_pSecond[2]; CCSprite* m_pCurChess; CCSprite*   m_pTargetChess; CHESS_TYPE  m_enCurChessType; CHESS_TYPE  m_enTarChessType; CCSprite* m_pChess[2][16]; CCMenuItemImage* m_pOpenVolice; CCMenuItemImage* m_pCloseVolice; //std::ofstream f_output; //std::string str; float  m_fPositionX; float  m_fPositionY; int   m_nChessTime; CCSize  s; bool  m_bSelect;   // has a chess be selected? if true, cannot be change bool  m_bVolice; bool  m_bCollectCorrect; int   ox, oy;    // the original coordinate of the current chess int   m_nContinuous;   // the max numbers to attact opponent's boss int   m_nCur;    // 0:red; 1:black int         m_nWin;    // 0:red; 1:black enum GAME_STATUS {  GAME_MENU,  GAME_RUNNING,  GAME_WIN,  GAME_OVER, }; GAME_STATUS m_enGameStatus; enum GAME_ROLE {  ROLE_RED,  ROLE_BLACK, }; GAME_ROLE m_enCurRole;  VEC_SPRITE m_vecSprite;};
oh,天,這個類太複雜了,對我們初學者來說,但是不要害怕,其他的我們先不管他是咋的,我們先來看基本的函式

一個是scene函式,一個是init函式。scene函式是建立函式,而init函式時初始化呼叫函式。


scene函式

CCScene* CCMainMenu::scene(){ CCScene* pScene = CCScene::create(); CCMainMenu* pLayer = CCMainMenu::create(); pScene->addChild(pLayer, 3); return pScene;}
scene函式和WelCome的scene函式類似,也是先建立一個scene,再建立一個CCMainMenu物件,然後把CCMainMenu物件加入到scene

最後返回scene

init函式

bool CCMainMenu::init(){ // 呼叫父類init函式 if (!CCLayer::init()) {  return false; } // 獲取視窗大小,這個視窗大小將來用來計算控制元件位置等 s = CCDirector::sharedDirector()->getWinSize(); // 建立背景圖片,並設定 m_pBKG = CCSprite::create(RES_PATH"background.png"); CCRect r = m_pBKG->getTextureRect(); m_pBKG->setAnchorPoint(CCPointZero); m_pBKG->setPosition(ccp(0, 0)); m_pBKG->setScaleX(s.width/r.size.width*0.667f); m_pBKG->setScaleY(s.height/r.size.height); this->addChild(m_pBKG, -2);  // 建立焦點圖片精靈,這個焦點是指棋子被選中 m_pFocus = CCSprite::create(RES_PATH"selected.png"); r = m_pFocus->getTextureRect(); m_pFocus->setScaleX(0.667f); m_pFocus->setScaleY(0.6f); m_pFocus->setVisible(false); this->addChild(m_pFocus, 1); m_fPositionX = s.width - r.size.width*0.667f; m_fPositionY = s.height - r.size.height*0.6f// 新局按鈕 CCMenuItemImage* pItem = CCMenuItemImage::create(RES_PATH"new.jpg", RES_PATH"new.jpg", this, menu_selector(CCMainMenu::menuCallBack)); pItem->setPosition(ccp(m_fPositionX - s.width/6, m_fPositionY - s.height/8)); pItem->setAnchorPoint(CCPointZero); pItem->setScaleX(0.667f); pItem->setScaleY(0.6f);  CCMenu* pMenu = CCMenu::create(pItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); // 悔棋按鈕 pItem = CCMenuItemImage::create(RES_PATH"regret.jpg", RES_PATH"regret.jpg", this, menu_selector(CCMainMenu::menuRegret)); pItem->setPosition(ccp(m_fPositionX - s.width/6, m_fPositionY - s.height/8*2)); pItem->setAnchorPoint(CCPointZero); pItem->setScaleX(0.667f); pItem->setScaleY(0.6f); pMenu = CCMenu::create(pItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); // 開始按鈕 pItem = CCMenuItemImage::create(RES_PATH"start.jpg", RES_PATH"start.jpg", this, menu_selector(CCMainMenu::menuStart)); pItem->setPosition(ccp(m_fPositionX - s.width/6, m_fPositionY - s.height/8*3)); pItem->setAnchorPoint(CCPointZero); pItem->setScaleX(0.667f); pItem->setScaleY(0.6f);// pMenu = CCMenu::create(pItem, NULL); xueguoliang pMenu = CCMenu::create(pItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); // 難度按鈕 pItem = CCMenuItemImage::create(RES_PATH"difficulty.jpg", RES_PATH"difficulty.jpg", this, menu_selector(CCMainMenu::menuHardChoose)); pItem->setPosition(ccp(m_fPositionX - s.width/6, m_fPositionY - s.height/8*4)); pItem->setAnchorPoint(CCPointZero); pItem->setScaleX(0.667f); pItem->setScaleY(0.6f); pMenu = CCMenu::create(pItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); // 聲音按鈕 m_pOpenVolice = CCMenuItemImage::create(RES_PATH"openVolice.png", RES_PATH"openVolice.png", this, menu_selector(CCMainMenu::menuSoundChoose)); CCSize r2 = m_pOpenVolice->getContentSize(); m_pOpenVolice->setPosition(ccp(m_fPositionX - s.width/6 + r.size.width/2 - r2.width/6, m_fPositionY - s.height/8*5)); m_pOpenVolice->setAnchorPoint(CCPointZero); m_pOpenVolice->setScaleX(0.667f); m_pOpenVolice->setScaleY(0.6f); pMenu = CCMenu::create(m_pOpenVolice, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1, 100); // 關閉聲音按鈕 m_pCloseVolice = CCMenuItemImage::create(RES_PATH"closeVolice.png", RES_PATH"closeVolice.png", this, menu_selector(CCMainMenu::menuSoundChoose)); m_pCloseVolice->setPosition(ccp(m_fPositionX - s.width/6 + r.size.width/2 - r2.width/6, m_fPositionY - s.height/8*5)); m_pCloseVolice->setAnchorPoint(CCPointZero); m_pCloseVolice->setScaleX(0.667f); m_pCloseVolice->setScaleY(0.6f); pMenu = CCMenu::create(m_pCloseVolice, NULL); pMenu->setPosition(CCPointZero); m_pCloseVolice->setVisible(false); this->addChild(pMenu, 1, 101); // 座標資訊 this->initCoordinate(); // 初始化時間 this->initTimeShow(); // 初始化圖片 this->initListImage();  this->setTouchEnabled(true); this->schedule(schedule_selector(CCMainMenu::updateTime), 1.0f); // 這個相當於做一個雙buffer,移動一個buffer,顯示一個buffer for (int i = 0; i < 10; ++i) {  for (int j = 0; j < 9; ++j)  {   g_cur_map[i][j] = g_chess_map[i][j];  } } // 一些遊戲中用到的狀態變數,需要的時候我們再詳細描述 m_enGameStatus  = GAME_MENU; m_enCurRole   = ROLE_RED; m_bVolice   = true; m_pCurChess   = NULL; m_bSelect   = false; m_bCollectCorrect = true; m_pTargetChess  = NULL; m_nContinuous  = 3; m_nCur    = 0; m_enCurChessType = CHESS_NONE; m_enTarChessType    = CHESS_NONE; m_nWin    = -1; m_vecSprite.reserve(10);  return true;}
init函式執行完之後,程式就顯示出主介面如下: