【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十二课:Cocos2D-X地图系统1-8

前端之家收集整理的这篇文章主要介绍了【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十二课:Cocos2D-X地图系统1-8前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

【麦可网】Cocos2d-X跨平台游戏开发---学习笔记

第二十二课:Cocos2D-X地图系统1-8

=======================================================================================================================================================================

课程目标:

-学习Cocos2D-X的地图系统

课程重点:

-地图编辑器的使用

-Cocos2D-X中的地图系统

考核目标:

-能够用地图编辑器编辑不同类型的地图

-能在Cocos2D-X中使用地图编辑器编辑出的地图

=======================================================================================================================================================================

一、两种不同类型的地图


二、地图载入

	CCTMXTiledMap* mapTile = CCTMXTiledMap::create("desert.tmx");
	this->addChild(mapTile);


三、动态更新

CCNode::reorderChild(CCNode *child,int zOrder);


四、例子

载入地图

添加精灵

添加菜单

如图:


bool HelloMap::init()
{
	//////////////////////////////
	// 1. super init first
	if ( !CCLayer::init() )
	{
		return false;
	}
	CCSize screenSize = CCDirector::sharedDirector()->getWinSize();

	mapTile = CCTMXTiledMap::create("map_desert.tmx");
	mapTile->setPosition(CCPointZero);
	this->addChild(mapTile);
	
	//SpinningPeas.png
	sprite = CCSprite::create("SpinningPeas.png");
	sprite->setPosition(ccp(	SPR_WIDTH/2,SPR_HEIGHT/2));
	mapTile->addChild(sprite);

	CCMenuItemFont* upMenuItem = CCMenuItemFont::create("UP",this,menu_selector(HelloMap::menuCallback));
	CCMenuItemFont* downMenuItem = CCMenuItemFont::create("DOWN",menu_selector(HelloMap::menuCallback));
	CCMenuItemFont* leftMenuItem = CCMenuItemFont::create("LEFT",menu_selector(HelloMap::menuCallback));
	CCMenuItemFont* rightMenuItem = CCMenuItemFont::create("RIGHT",menu_selector(HelloMap::menuCallback));
	CCMenuItemFont* updateMenuItem = CCMenuItemFont::create("UPDATE",menu_selector(HelloMap::menuCallback));

	CCMenu* menu = CCMenu::create(upMenuItem,downMenuItem,leftMenuItem,rightMenuItem,updateMenuItem,NULL);
	menu->setPosition(CCPointZero);
	this->addChild(menu);

	CCLOG("height=%f",screenSize.height);
	CCLOG("width=%f",screenSize.width);

	upMenuItem->setPosition(ccp(screenSize.width/2,screenSize.height-40));
	downMenuItem->setPosition(ccp(screenSize.width/2,40));
	leftMenuItem->setPosition(ccp(40,screenSize.height/2));
	rightMenuItem->setPosition(ccp(screenSize.width-40,screenSize.height/2));
	updateMenuItem->setPosition(ccp(screenSize.width/2,screenSize.height/2));

	upMenuItem->setTag(TAG_UP);
	downMenuItem->setTag(TAG_DOWN);
	leftMenuItem->setTag(TAG_LEFT);
	rightMenuItem->setTag(TAG_RIGHT);
	updateMenuItem->setTag(TAG_UPDATE);

	return true;
}

-----------------------------------------------
UP、DOWN、LEFT、RIGHT、UPDATE对应的回调函数
void HelloMap::menuCallback(CCObject* pSender)
{
//获取屏幕尺寸
	CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
//获得菜单标记
	CCMenuItem* item = (CCMenuItem*)pSender;
	int tag = item->getTag();

//获得当前位置和要移动的下个位置
	CCPoint spriteCurPos = ccp( (int)(sprite->getPositionX()/SPR_WIDTH),mapTile->getMapSize().height-1 - (int)(sprite->getPositionY()/SPR_HEIGHT) );
	CCPoint spriteNextPos = spriteCurPos;
	CCPoint flagPos = CCPointZero;

//获取图层
	CCTMXLayer* layer2 = mapTile->layerNamed("layer2");
	int gID = 0;

	switch(tag)
	{
//上移动
	case TAG_UP:
//越界返回
		spriteNextPos.y -= 1;
		if (spriteNextPos.y < 0) return;
		
//碰撞检测
		gID = layer2->tileGIDAt(spriteNextPos);
		if (gID) return;		

//移动地图和精灵
//1.如果 地图高度+地图Y位置 > 屏幕高度 (精灵Y位置 > 地图高度/2 滚屏,否则只		移动精灵)。
//2. 不满足1且精灵Y位置 < 地图高度 移动精灵
		if ( mapTile->getContentSize().height + mapTile->getPositionY() > screenSize.height + SPR_HEIGHT)
		{
			if (sprite->getPositionY() > screenSize.height/2)
			{
				mapTile->setPositionY(mapTile->getPositionY() - SPR_HEIGHT);
			}
			sprite->setPosition(ccp(	sprite->getPositionX(),sprite->getPositionY()+SPR_HEIGHT));
		}
		else if (sprite->getPositionY() < mapTile->getContentSize().height-SPR_HEIGHT)
		{
			sprite->setPosition(ccp(	sprite->getPositionX(),sprite->getPositionY()+SPR_HEIGHT));
		}
		break;

	case TAG_DOWN:
		spriteNextPos.y += 1;
		if (spriteNextPos.y > mapTile->getMapSize().height-1) return;

		gID = layer2->tileGIDAt(spriteNextPos);
		if (gID) return;

		if (mapTile->getPositionY() < 0)
		{
			if (sprite->getPositionY() < mapTile->getContentSize().height - screenSize.height/2)
			{
				mapTile->setPositionY(mapTile->getPositionY() + SPR_HEIGHT);
			}
			sprite->setPosition(ccp(	sprite->getPositionX(),sprite->getPositionY()-SPR_HEIGHT));
		}
		else if (sprite->getPositionY() > SPR_HEIGHT)
		{
			sprite->setPosition(ccp(	sprite->getPositionX(),sprite->getPositionY()-SPR_HEIGHT));
		}
		break;

	case TAG_LEFT:
		spriteNextPos.x -= 1;
		if (spriteNextPos.x < 0) return;

		gID = layer2->tileGIDAt(spriteNextPos);
		if (gID) return;

		if (mapTile->getPositionX() < 0)
		{
			if (sprite->getPositionX() < mapTile->getContentSize().width - screenSize.width/2)
			{
				mapTile->setPositionX(mapTile->getPositionX() + SPR_WIDTH);
			}
			sprite->setPosition(ccp(	sprite->getPositionX()-SPR_WIDTH,sprite->getPositionY()));
		}
		else if (sprite->getPositionX() > SPR_WIDTH)
		{
			sprite->setPosition(ccp(	sprite->getPositionX()-SPR_WIDTH,sprite->getPositionY()));
		}
		break;

	case TAG_RIGHT:
		spriteNextPos.x += 1;
		if (spriteNextPos.x > mapTile->getMapSize().width-1 ) return;

		gID = layer2->tileGIDAt(spriteNextPos);
		if (gID) return;

		if ( mapTile->getContentSize().width + mapTile->getPositionX() > screenSize.width + SPR_HEIGHT)
		{
			if (sprite->getPositionX() > screenSize.width/2)
			{
				mapTile->setPositionX(mapTile->getPositionX() - SPR_WIDTH);
			}
			sprite->setPosition(ccp(	sprite->getPositionX()+SPR_WIDTH,sprite->getPositionY()));
		}
		else if (sprite->getPositionX() < mapTile->getContentSize().width-SPR_WIDTH)
		{
			sprite->setPosition(ccp(	sprite->getPositionX()+SPR_WIDTH,sprite->getPositionY()));
		}
		break;
	case TAG_UPDATE:
//动态更新
		flagPos.x = spriteCurPos.x + 1;
		flagPos.y = spriteCurPos.y;
		layer2->setTileGID(46,flagPos);
		break;
	}
}

===================================================================

总结:

这一课有趣,有地图有角色啦。

开心一刻:

有两个神经病患.从病院里逃出来.

两人跑啊跑.爬到一棵树上.

其中一个人从树上跳下来.

滚啊滚的.

然後抬起头对上面的人说:------你怎么还不下来啊---------?

上面的那个人回答他:------------

我还没有熟-----------


==================================================================

【麦可网】Cocos2d-X跨平台游戏开发---教程下载:http://pan.baidu.com/s/1kTio1Av

【麦可网】Cocos2d-X跨平台游戏开发---笔记系列:http://blog.csdn.net/qiulanzhu

原文链接:/cocos2dx/342402.html

猜你在找的Cocos2d-x相关文章