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

cocos2d-x游戏实例 《简单棋》入门尝试(三) 初始化棋子布局信息

2013-02-10 20:02 656 查看

一、添加类Chessman

1、功能分析与实现

1.1: 功能1
1.1.1 分析

程序初始化的时候,为了获取map中标示出的21个对象点的相关信息。在程序实现中,定义了数组struct _PointInfo pointsInfo[21];来存储这21个坐标点的信息(为了便于后面外部文件的调用,定义的是全局变量)。其结构体_PointInfo定义的是map坐标点的相关信息,其在定义的头文件common.h中声明。

1.1.2 实现

(1)添加头文件common.h,其头文件代码如下:

#pragma once

#include "cocos2d.h"
using namespace cocos2d;

//棋盘中一个棋子坐标点的信息
typedef struct _PointInfo
{
CCSprite *currentSprite;		//当前棋子精灵对象(如果不存在,则为NULL)
bool isRed;				//当前棋子是红棋还是黑棋,红棋为true
CCPoint currentPoint;			//当前坐标值
bool isNotEmpty;			//当前点是否为空(即没有棋子图片)
int nearLocations[4];			//当前棋子周围的棋子位置,不满四个的后面两个值设置为-1

_PointInfo()
{
currentSprite = NULL;
isRed = false;
currentPoint.x = 0;
currentPoint.y = 0;
isNotEmpty = false;					//默认为NULL
for (int i = 0; i < 4; i++)
{
nearLocations[i] = -1;
}
}
}*_pPointInfo;

class Common
{

};
currentSprite存储的是某个对象点(如map中对象层的c1对象)当前创建的精灵对象,如在c1位置放置的一个棋子图片(精灵)。如果此时该点没有放置精灵,则将其置为NULL;

isRed存储的是当前图片(精灵)是红色的还是蓝色的,即不同玩家;

currentPoint存储的是该对象点的坐标;

isNotEmpty存储的是该对象点是否存在图片,其也可以用currentSprite是否为NULL来判断,但考虑程序处理的方便,还是定义了该变量;

nearLocations[4]存放的是该对象点周围的邻结点,如地图map中c1对象的邻结点为c2、c12、c13,其不存在四个邻结点,则nearLocations[3]默认为-1。而为何定义的是int呢?其是对应定义的数组pointsInfo的下标值。因pointsInfo数组下标是0-20,而map中对象是1-21,故(nearLocations的值) = (pointsInfo数组的下标值+1);

(2)添加类Chessman,在Chessman.cpp中加入common.h头文件以及定义全局变量。

#include "Common.h"
struct _PointInfo pointsInfo[21];			//用于存储棋盘中21个坐标点的数组


1.2:功能2

1.2.1 分析
在程序加载的时候,map中每个对象点的坐标以及周围的邻接点都是已知的。所以初始化的时候就加载这些数据。定义了相应函数实现其功能(详见1.2.2实现)。
1.2.2 实现
(1)在Chessman.h中加入头文件
#include "cocos2d.h"
using namespace cocos2d;

(2)声明函数如下:
public:
//初始化棋子布局
static void InitChessmanWithMap(CCTMXTiledMap *map);

private:
/*
**	功能:	设置所有坐标点的坐标
**	map:	加载的地图
*/
void SetAllPointsLocation( CCTMXTiledMap *map);

/*
**	功能:	获取一个坐标点的坐标
**	name:	map对象层中的对象名
*/
CCPoint GetOnePointLocation(CCTMXObjectGroup* objGroup,const char *name);

/*
**	功能:	设置map中所有对象点的数据,即结构体_PointInfo
*/
void SetAllPointsData(CCTMXTiledMap *map);

/*
**	功能:		设置一个对象点的数据
**	location:	该对象点对应的数组下表值
**	path:		添加的精灵的图片路径,为NULL时其不添加精灵
**	isRed:		如果加载精灵图片,根据isRed判断该位置是哪方玩家
*/
void SetOnePointData(int location, char *path, CCTMXTiledMap *map, bool isRed);

/*
**	功能:	设置当前结点周围的结点,当为-1时,表示不存在该结点(有些点只有三个邻结点)
*/
void SetNearPointLocation(int currentPoint, int nearPoint1 = -1, int nearPoint2 = -1, int nearPoint3 = -1, int nearPoint4 = -1);
(3)函数实现如下:
void Chessman::InitChessmanWithMap(CCTMXTiledMap *map)
{
Chessman* mPlayer = new Chessman();
mPlayer->SetAllPointsLocation(map);

mPlayer->SetAllPointsData(map);
}

void Chessman::SetAllPointsLocation(CCTMXTiledMap *map)
{
CCTMXObjectGroup* objGroup = map->objectGroupNamed("chessmansObject");	//加载对象层
char *name[21] = {"c1","c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11", "c12", "c13", "c14", "c15", "c16", "c17", "c18", "c19", "c20", "c21"};
for (int i = 0; i < 21; i++)
{
pointsInfo[i].currentPoint =  GetOnePointLocation(objGroup, name[i]);
}
}

CCPoint Chessman::GetOnePointLocation(CCTMXObjectGroup* objGroup,const char *name)
{
CCDictionary* playerPointDic = objGroup->objectNamed(name);
CCPoint cp;
cp.x = playerPointDic->valueForKey("x")->floatValue() + 15;		//在设置获取的坐标还加15个点的偏移(这样的位置看着比较顺眼,在加载了棋子之后)
cp.y = playerPointDic->valueForKey("y")->floatValue() + 15;
return cp;
}

void Chessman::SetAllPointsData(CCTMXTiledMap *map)
{
SetOnePointData(1, "image/blue.png", map, false);
SetNearPointLocation(1, 2, 12, 13);
SetOnePointData(2, "image/blue.png",  map, false);
SetNearPointLocation(2, 1, 3, 13);
SetOnePointData(3, "image/blue.png",  map, false);
SetNearPointLocation(3, 2, 4, 14);
SetOnePointData(11, "image/blue.png", map, false);
SetNearPointLocation(11, 10, 12, 16);
SetOnePointData(12, "image/blue.png",map, false);
SetNearPointLocation(12, 1, 11, 13);
SetOnePointData(13, "image/blue.png", map, false);
SetNearPointLocation(13, 1, 2, 12, 17);

SetOnePointData(5, "image/red.png",  map, true);
SetNearPointLocation(5, 4, 6, 14);
SetOnePointData(6, "image/red.png",  map, true);
SetNearPointLocation(6, 5, 7, 15);
SetOnePointData(7, "image/red.png",  map, true);
SetNearPointLocation(7, 6, 8, 15);
SetOnePointData(8, "image/red.png", map, true);
SetNearPointLocation(8, 7, 9, 15);
SetOnePointData(9, "image/red.png", map, true);
SetNearPointLocation(9, 8, 10, 16);
SetOnePointData(15, "image/red.png", map, true);
SetNearPointLocation(15, 6, 7, 8, 19);

SetOnePointData(4, NULL,  map, false);
SetNearPointLocation(4, 3, 5, 14);
SetOnePointData(10, NULL,  map, false);
SetNearPointLocation(10, 9, 11, 16);
SetOnePointData(14, NULL,  map, false);
SetNearPointLocation(14, 3, 4, 5, 18);
SetOnePointData(16, NULL,  map, false);
SetNearPointLocation(16, 9, 10, 11, 20);
SetOnePointData(17, NULL,  map, false);
SetNearPointLocation(17, 13, 18, 20, 21);
SetOnePointData(18, NULL,  map, false);
SetNearPointLocation(18, 14, 17, 19, 21);
SetOnePointData(19, NULL,  map, false);
SetNearPointLocation(19, 15, 18, 20, 21);
SetOnePointData(20, NULL,  map, false);
SetNearPointLocation(20, 16, 17, 19, 21);
SetOnePointData(21, NULL,  map, false);
SetNearPointLocation(21, 17, 18, 19, 20);
}

void Chessman::SetOnePointData(int location, char *path, CCTMXTiledMap *map, bool isRed)
{
location = location - 1;

if(path == NULL)
{
//在初始化的时候不加入图片
pointsInfo[location].currentSprite = NULL;
pointsInfo[location].isNotEmpty = false;
}
else
{
CCSprite *playerSprite = CCSprite::create(path);
//playerSprite->setScale(0.2);//缩放
playerSprite->setPosition(pointsInfo[location].currentPoint);
map->addChild(playerSprite);										//精灵添加到地图
pointsInfo[location].currentSprite = playerSprite;					//设置参数
pointsInfo[location].isNotEmpty = true;
pointsInfo[location].isRed = isRed;
}
}

void Chessman::SetNearPointLocation(int currentPoint, int nearPoint1, int nearPoint2, int nearPoint3, int nearPoint4)
{
pointsInfo[currentPoint - 1].nearLocations[0] = nearPoint1;
pointsInfo[currentPoint - 1].nearLocations[1] = nearPoint2;
pointsInfo[currentPoint - 1].nearLocations[2] = nearPoint3;
pointsInfo[currentPoint - 1].nearLocations[3] = nearPoint4;
}

(4)在ChessScene.cpp加入Chessman.h头文件。并调用其相关函数。在init中添加代码:
//初始化棋子
Chessman::InitChessmanWithMap(map);

二、相应资源下载

相应的棋子图片下载

三、程序执行

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐