您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x游戏开发系列教程-中国象棋05-开始游戏

2013-12-29 00:18 591 查看

前情回顾

通过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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: