1. 程式人生 > >cocos2d-x遊戲開發系列教程-中國象棋05-開始遊戲

cocos2d-x遊戲開發系列教程-中國象棋05-開始遊戲

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

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

               

前情回顧

通過CCMainMenu的init函式,已經把所有的按鈕,棋子都擺放完畢了,但是這個時候,棋子是不能走動的,只有在開始遊戲之後才能移動棋子。

點選按鈕,開始遊戲,那麼點選開始按鈕之後,程式究竟發生了什麼事,我們繼續看程式碼到建立這個開始按鈕的地方。

開始按鈕的建立

開始按鈕的建立程式碼,在上一篇博文中有所提起,但是沒有重點提及,是在CCMainMenu::init()函式中

// 開始按鈕 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);
從上面一段建立開始按鈕的程式碼中,在第一句建立item的程式碼中,我們見到在第一句中,menu_selector括號中的CCMainMenu::menuStart

所指是啟動按鈕被點選時,menuStart函式將會被呼叫。


menuStart函式

void CCMainMenu::menuStart(CCObject* pSender){ if(m_enGameStatus != GAME_MENU) {  return; } this->schedule(schedule_selector(CCMainMenu::updateFocus), 0.5f); m_enGameStatus  = GAME_RUNNING; m_nChessTime = 600this->setNumberSprite(m_nChessTime);}
menuStart函式,主要修改遊戲狀態為GAME_RUNNING,這個狀態讓棋子點選有效。


ccTouchesEnded

這個函式是滑鼠點選函式,沒走一步棋子至少需要滑鼠點選兩次,一次是選中,一次是移動。都是在這個函式裡處理

void CCMainMenu::ccTouchesEnded(CCSet* pTouches, CCEvent* pEvent){ if(m_enGameStatus != GAME_RUNNING) {  return; } CCSetIterator it = pTouches->begin(); CCTouch* pTouch = (CCTouch*)(*it); CCPoint touchPoint = pTouch->getLocationInView(); touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint); this->dealWithChess(touchPoint.x, touchPoint.y);}
這個函式首先判斷現在遊戲狀態,是不是GAME_RUNNING,如果不是,則直接返回
否則,獲取座標點,然後呼叫dealWithChess函式

dealWithChess

dealWithChess函式時已經確定了在遊戲中時,滑鼠點選了棋盤中的點

這個函式負責棋子選中和移動的邏輯,詳細的程式碼和註釋見如下:

void CCMainMenu::dealWithChess(float x, float y){ // 判斷是不是棋盤範圍 if (x < 24 || x > 294 || y < 14 || y > 312) {  return; } CCSprite* pSprite;  // 獲取該座標點上的棋子 pSprite = this->getChessByCoord(x, y); if(!this->getChessByCoord(x, y, 1))  {  // can not find coordinate  return; } int x0 = static_cast<int>(x); int y0 = static_cast<int>(y); if (pSprite)  // 如果點中了每個棋子 {  if(m_bSelect)  // 如果之前有選中某個棋子  {   m_pTargetChess = pSprite; // 那麼這次選中的棋子是目標  }  else  // 如果沒選中,那麼這次點的棋子是選中的棋子  {   if (!m_nCur && m_enCurChessType < CHESS_BORDER)   {    return;   }   else if(m_nCur && m_enCurChessType > CHESS_BORDER)   {    return;   }   m_pFocus->setPosition(pSprite->getPosition());   m_pFocus->setVisible(true);   m_pCurChess = pSprite;   ox = x0;   oy = y0;   m_bSelect = false;  } } if (m_bSelect) {   if(!this->judgeAction(x0, y0))  {   this->clean();   return;  }  if((m_enCurChessType < CHESS_BORDER && m_enTarChessType < CHESS_BORDER && m_enTarChessType > CHESS_NONE) ||    (m_enCurChessType > CHESS_BORDER && m_enTarChessType > CHESS_BORDER))  {   this->clean();   return;  }  if(m_pTargetChess && m_pTargetChess != m_pCurChess)  {   m_pTargetChess->setVisible(false);   this->collectInfo(m_pTargetChess);  }  this->collectInfo(m_pCurChess);  CCPoint p = g_chess_coord[x0][y0];  m_pCurChess->setPosition(p);  m_pFocus->setPosition(p);  g_cur_map[x0][y0] = g_cur_map[ox][oy];  g_cur_map[ox][oy] = 0;  //this->print();  m_nCur = m_nCur == 0 ? 1 : 0;  this->clean();  this->judgeWin(); } else {  m_bSelect = true; }}




           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述