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

一、添加类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);

二、相应资源下载

三、程序执行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值