分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net
前情回顾通过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 = 600; this->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; }}
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net
总结以上是内存溢出为你收集整理的cocos2d-x游戏开发系列教程-中国象棋05-开始游戏全部内容,希望文章能够帮你解决cocos2d-x游戏开发系列教程-中国象棋05-开始游戏所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)