前情回顾

在之前的学习中,我们已经了解到,下棋主界面是由CCMainMenu类实现的,在它的init函数中,初始化了

主界面需要的各种数据,包括:创建控件,初始化32个棋子,初始化执行变量等等,在这个博文中,我们

主要来学习,32个棋子是如何被初始化并显示在界面上的。这个显示和initCoordinate以及

了解32个棋子显示在界面的目的,是为了让大家更好理解32个棋子的数据结构,这些数据结构在当棋子走动

时,需要改动,所以必须要理解。

initCoordinate

void CCMainMenu::initCoordinate()
{
/*std::string str = "../coordinate.txt";
std::ofstream f_output;
f_output.open(str.c_str(), std::ios::app);*/
float xSpan = 48.0f*0.667f;
float ySpan = 32.0f*0.6f;
float xStart = 1;
float yStart = xSpan;
for (int i = 0, x = 9; i < 10; ++i, --x)
{
for (int j = 0, y = 1; j < 9; ++j, ++y)
{
g_chess_coord[i][j] = ccp(xStart + xSpan*y, yStart*x + ySpan); // 计算象棋坐标点的坐标,把象棋坐标转化成view坐标
/*f_output<< "[" << i << "][" << j <<"]: (" << xStart + xSpan*y << ", " << yStart*x + ySpan << ")" << " ";
if(j == 4 || j == 8)
{
f_output<<std::endl;
}*/
}
/*f_output<<std::endl;*/
}
/*f_output.close();*/
}

这个函数功能是将象棋的坐标转化成屏幕坐标,并保存在g_chess_coord中。有了这些坐标,为后面的摆棋工作打下基础。

initListImage

initListImage函数,初始化了32个棋子的位置,代码的逻辑很简单,就是调用32次getListSprite,获得32个棋子精灵并保存在m_pChess中

然后调用initChessPosition去设置32个棋子的位置,代码在MainMenu.cpp中

void CCMainMenu::initListImage()
{
// red
m_pChess[1][0] = this->getListSprite(CHESS_RK);
this->addChild(m_pChess[1][0], 0); m_pChess[1][1] = this->getListSprite(CHESS_RA);
this->addChild(m_pChess[1][1], 0); m_pChess[1][2] = this->getListSprite(CHESS_RA);
this->addChild(m_pChess[1][2], 0); m_pChess[1][3] = this->getListSprite(CHESS_RB);
this->addChild(m_pChess[1][3], 0); m_pChess[1][4] = this->getListSprite(CHESS_RB);
this->addChild(m_pChess[1][4], 0); m_pChess[1][5] = this->getListSprite(CHESS_RN);
this->addChild(m_pChess[1][5], 0); m_pChess[1][6] = this->getListSprite(CHESS_RN);
this->addChild(m_pChess[1][6], 0); m_pChess[1][7] = this->getListSprite(CHESS_RR);
this->addChild(m_pChess[1][7], 0); m_pChess[1][8] = this->getListSprite(CHESS_RR);
this->addChild(m_pChess[1][8], 0); m_pChess[1][9] = this->getListSprite(CHESS_RC);
this->addChild(m_pChess[1][9], 0); m_pChess[1][10] = this->getListSprite(CHESS_RC);
this->addChild(m_pChess[1][10], 0); m_pChess[1][11] = this->getListSprite(CHESS_RP);
this->addChild(m_pChess[1][11], 0); m_pChess[1][12] = this->getListSprite(CHESS_RP);
this->addChild(m_pChess[1][12], 0); m_pChess[1][13] = this->getListSprite(CHESS_RP);
this->addChild(m_pChess[1][13], 0); m_pChess[1][14] = this->getListSprite(CHESS_RP);
this->addChild(m_pChess[1][14], 0); m_pChess[1][15] = this->getListSprite(CHESS_RP);
this->addChild(m_pChess[1][15], 0); //black
m_pChess[0][0] = this->getListSprite(CHESS_BK);
this->addChild(m_pChess[0][0], 0); m_pChess[0][1] = this->getListSprite(CHESS_BA);
this->addChild(m_pChess[0][1], 0); m_pChess[0][2] = this->getListSprite(CHESS_BA);
this->addChild(m_pChess[0][2], 0); m_pChess[0][3] = this->getListSprite(CHESS_BB);
this->addChild(m_pChess[0][3], 0); m_pChess[0][4] = this->getListSprite(CHESS_BB);
this->addChild(m_pChess[0][4], 0); m_pChess[0][5] = this->getListSprite(CHESS_BN);
this->addChild(m_pChess[0][5], 0); m_pChess[0][6] = this->getListSprite(CHESS_BN);
this->addChild(m_pChess[0][6], 0); m_pChess[0][7] = this->getListSprite(CHESS_BR);
this->addChild(m_pChess[0][7], 0); m_pChess[0][8] = this->getListSprite(CHESS_BR);
this->addChild(m_pChess[0][8], 0); m_pChess[0][9] = this->getListSprite(CHESS_BC);
this->addChild(m_pChess[0][9], 0); m_pChess[0][10] = this->getListSprite(CHESS_BC);
this->addChild(m_pChess[0][10], 0); m_pChess[0][11] = this->getListSprite(CHESS_BP);
this->addChild(m_pChess[0][11], 0); m_pChess[0][12] = this->getListSprite(CHESS_BP);
this->addChild(m_pChess[0][12], 0); m_pChess[0][13] = this->getListSprite(CHESS_BP);
this->addChild(m_pChess[0][13], 0); m_pChess[0][14] = this->getListSprite(CHESS_BP);
this->addChild(m_pChess[0][14], 0); m_pChess[0][15] = this->getListSprite(CHESS_BP);
this->addChild(m_pChess[0][15], 0); this->initChessPosition();
}

getListSprite

这个函数负责创建棋子精灵,根据不同的棋子类型,使用不同的图片创建精灵,在你们自己的环境中,尤其注意图片路径

图片路劲不对,那什么都不显示,所以如果有错,先考虑是不是图片路径不对了。

CCSprite* CCMainMenu::getListSprite(CHESS_TYPE nType)
{
CCSprite* pSprite;
switch(nType)
{
case CHESS_RK:
pSprite = CCSprite::create(RES_PATH"rk.png");
break;
case CHESS_RA:
pSprite = CCSprite::create(RES_PATH"ra.png");
break;
case CHESS_RB:
pSprite = CCSprite::create(RES_PATH"rb.png");
break;
case CHESS_RN:
pSprite = CCSprite::create(RES_PATH"rn.png");
break;
case CHESS_RR:
pSprite = CCSprite::create(RES_PATH"rr.png");
break;
case CHESS_RC:
pSprite = CCSprite::create(RES_PATH"rc.png");
break;
case CHESS_RP:
pSprite = CCSprite::create(RES_PATH"rp.png");
break;
case CHESS_BK:
pSprite = CCSprite::create(RES_PATH"bk.png");
break;
case CHESS_BA:
pSprite = CCSprite::create(RES_PATH"ba.png");
break;
case CHESS_BB:
pSprite = CCSprite::create(RES_PATH"bb.png");
break;
case CHESS_BN:
pSprite = CCSprite::create(RES_PATH"bn.png");
break;
case CHESS_BR:
pSprite = CCSprite::create(RES_PATH"br.png");
break;
case CHESS_BC:
pSprite = CCSprite::create(RES_PATH"bc.png");
break;
case CHESS_BP:
pSprite = CCSprite::create(RES_PATH"bp.png");
break;
} pSprite->setScaleX(0.5f);
pSprite->setScaleY(0.5f);
return pSprite;
}

initChessPosition

initListImage最后调用的是initChessPosition,为32个棋子指定位置。这里位置信息用到了g_chess_coord,这个坐标信息是之前函数

void CCMainMenu::initChessPosition()
{
m_pChess[1][0]->setPosition(g_chess_coord[9][4]);
m_pChess[1][1]->setPosition(g_chess_coord[9][3]);
m_pChess[1][2]->setPosition(g_chess_coord[9][5]);
m_pChess[1][3]->setPosition(g_chess_coord[9][2]);
m_pChess[1][4]->setPosition(g_chess_coord[9][6]);
m_pChess[1][5]->setPosition(g_chess_coord[9][1]);
m_pChess[1][6]->setPosition(g_chess_coord[9][7]);
m_pChess[1][7]->setPosition(g_chess_coord[9][0]);
m_pChess[1][8]->setPosition(g_chess_coord[9][8]);
m_pChess[1][9]->setPosition(g_chess_coord[7][1]);
m_pChess[1][10]->setPosition(g_chess_coord[7][7]);
m_pChess[1][11]->setPosition(g_chess_coord[6][0]);
m_pChess[1][12]->setPosition(g_chess_coord[6][2]);
m_pChess[1][13]->setPosition(g_chess_coord[6][4]);
m_pChess[1][14]->setPosition(g_chess_coord[6][6]);
m_pChess[1][15]->setPosition(g_chess_coord[6][8]);
//black
m_pChess[0][0]->setPosition(g_chess_coord[0][4]);
m_pChess[0][1]->setPosition(g_chess_coord[0][3]);
m_pChess[0][2]->setPosition(g_chess_coord[0][5]);
m_pChess[0][3]->setPosition(g_chess_coord[0][2]);
m_pChess[0][4]->setPosition(g_chess_coord[0][6]);
m_pChess[0][5]->setPosition(g_chess_coord[0][1]);
m_pChess[0][6]->setPosition(g_chess_coord[0][7]);
m_pChess[0][7]->setPosition(g_chess_coord[0][0]);
m_pChess[0][8]->setPosition(g_chess_coord[0][8]);
m_pChess[0][9]->setPosition(g_chess_coord[2][1]);
m_pChess[0][10]->setPosition(g_chess_coord[2][7]);
m_pChess[0][11]->setPosition(g_chess_coord[3][0]);
m_pChess[0][12]->setPosition(g_chess_coord[3][2]);
m_pChess[0][13]->setPosition(g_chess_coord[3][4]);
m_pChess[0][14]->setPosition(g_chess_coord[3][6]);
m_pChess[0][15]->setPosition(g_chess_coord[3][8]);
}

好,当程序完全执行完这些代码后,就完成了32个棋子的初始化。

下一遍博文我们讲讲述这个象棋是怎么走起来

cocos2d-x游戏开发系列教程-中国象棋04-摆棋的更多相关文章

  1. cocos2d-x游戏开发系列教程-中国象棋00-前言

    象棋描述 在说代码之前,我们先让象棋效果登场,以方便大家对代码的理解 欢迎界面 中国象棋程序,运行起来的第一个界面是一个欢迎界面,该欢迎界面在停留一秒后进入游戏界面 游戏主界面 新局:所有棋子归位,状 ...

  2. cocos2d-x游戏开发系列教程-中国象棋02-main函数和欢迎页面

    之前两个博客讲述了象棋的规格和工程文件之后,我们继续深入的从代码开始学习cocos2dx 首先从程序入口main函数开始 main函数 int APIENTRY _tWinMain(HINSTANCE ...

  3. cocos2d-x游戏开发系列教程-中国象棋01-工程文件概述

    上一篇博文我们看到了象棋的效果图,这一张我们来看象棋代码的整体概述 让我们先对整个代码框架有个了解. 主目录: 主目录包含内容如上图: classes目录:业务代码 proj.win32:包括main ...

  4. cocos2d-x游戏开发系列教程-中国象棋03-主界面

    前情回顾 上个博客说道我们象棋程序进入了欢迎界面,在欢迎界面下等待一秒进入主界面 进入主界面的关键代码如下: CCScene* pScene = CCMainMenu::scene();  创建sce ...

  5. cocos2d-x游戏开发系列教程-中国象棋06-游戏规则

    前情回顾 上一个博文我们提到象棋运动的函数dealWithChess,但是只是说该函数完成了棋子的选择和移动功能 其实在这个函数里,在移动棋子之前,是要对棋子的移动是否合法进行判断的,我们一起来看看如 ...

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

    前情回顾 通过CCMainMenu的init函数,已经把所有的按钮,棋子都摆放完毕了,但是这个时候,棋子是不能走动的,只有在开始游戏之后才能移动棋子. 点击

  7. HTML5游戏开发系列教程7(译)

    原文地址:http://www.script-tutorials.com/html5-game-development-lesson-7/ 今天我们将完成我们第一个完整的游戏--打砖块.这次教程中,将 ...

  8. HTML5游戏开发系列教程6(译)

    原文地址:http://www.script-tutorials.com/html5-game-development-lesson-6/ 这是我们最新一篇HTML5游戏开发系列文章.我们将继续使用c ...

  9. HTML5游戏开发系列教程5(译)

    原文地址:http://www.script-tutorials.com/html5-game-development-lesson-5/ 最终我决定准备下一篇游戏开发系列的文章,我们将继续使用can ...

随机推荐

  1. js获取浏览器地址栏传递的参数

    function getQueryString(key){ var href=window.location.href; var reg = new RegExp(key +"=([^&am ...

  2. <META http-equiv=Content-Type content="text/html; charset=gb2312">

    META,网页Html语言里Head区重要标签之一 HTTP-EQUIV类似于HTTP的头部协议,它回应给浏览器一些有 用的信息,以帮助正确和精确地显示网页内容.常用的HTTP- EQUIV类型有: ...

  3. PowerShell入门(序):为什么需要PowerShell?

    原文:http://www.cnblogs.com/ceachy/archive/2013/01/23/PowerShellPreface.html 曾几何时,微软的服务器操作系统因为缺乏一个强大的S ...

  4. JS 移动动画

    function moveElement(elementId, final_x, final_y,interval) {            if (!document.getElementById ...

  5. perl 自动发产品

    use Net::SMTP; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Headers; use HTTP::Response; use Enc ...

  6. [上海] 携程 门票事业部 招聘.NET 架构师 2 名 - V2EX

    [上海] 携程 门票事业部 招聘.NET 架构师 2 名 - V2EX [上海] 携程 门票事业部 招聘.NET 架构师 2 名

  7. (step6.3.5)hdu 1281(棋盘游戏——二分图的完美匹配)

    题目大意:本体是中文题.读者可以直接在OJ上看 解题思路: 1)完美匹配:所有的端点都是匹配点 2)对于二分图的完美匹配,我们需要用一个数组来存储匹配点.(而二分图的其他问题(我们则可以直接使用变量来 ...

  8. stl源代码剖析:编译器的提前定义位置集设置

    眼下我的工作环境还是win.全部演示也用VS或者cygwin这些环境作为基础. 1.配置项目的附加include目,添加提前定义位置集设置,编译器会把它添加include路径,比方在某个目录中定义一个 ...

  9. Android中通过进程注入技术改动广播接收器的优先级

    前言 这个周末又没有吊事,在家研究了怎样通过进程的注入技术改动广播接收器的优先级.关于这个应用场景是非常多的.并且也非常重要.所以就非常急的去fixed了. Android中的四大组件中有一个广播:B ...

  10. Handler总结

    一.整体工程图 二.activity_handler.xml <?xml version="1.0" encoding="utf-8"?> < ...