cocos2d-x游戏开发系列教程-超级玛丽07-CMGameMap(二)


在了解地图的初始化和加载之前,我们先了解下mario的地图。

用tiled工具打开mario地图

从地图中可以看到,mario的地图有很多层构成:

objects层:怪物,会动的怪物

coin层:金币

trap层:陷阱

block层:砖头,砖头有好几种,带问号的,普通砖头,拦路的等等

pipe层:水管

flagpole层:终点旗帜

land层:承重的路面

mountain层:远处的山

cloud层:天上的云

background层:背景层


land,mountain,cloud,background,pipe这些层都不需要变化,只是需要移动罢了

金币,砖头,怪物这些层的精灵,需要移动或者消失,处理不相同


在init函数中,我们看怪物的初始化吧,以下代码是初始化怪物的代码,我们一句句来分析这些代码

//初始化怪物显示
		CCTMXObjectGroup* pObjectLayer = objectGroupNamed("objects");
		CC_BREAK_IF(pObjectLayer==NULL);
		CCArray *ObjectArray = pObjectLayer->getObjects();
		CCDictionary *pDic = NULL;
		for (unsigned int i = 0; i < ObjectArray->count(); i++)
		{
			pDic = (CCDictionary *)ObjectArray->objectAtIndex(i);
			int PosX = ((CCString*)pDic->objectForKey("x"))->intValue();
			int PosY = ((CCString*)pDic->objectForKey("y"))->intValue();
			PosY -= this->getTileSize().height;
			CCPoint TileXY = ccp(PosX, PosY);

			CCString *strName = (CCString*)pDic->objectForKey("name");
			CCString *strType = (CCString*)pDic->objectForKey("type");

			// 进行怪物的初始化,先根据名字来判断是不是enemy,再细分enemy类型		
			if (strName->m_sString == "enemy")
			{
				if (strType->m_sString == "mushroom")
				{
					CMMonsterMushrooms *pMonster = CMMonsterMushrooms::CreateMonsterMushrooms(TileXY,pMario,this,this);
					if (pMonster==NULL)
					{
						CCLog("pMonster==NULL!");
					}
					pMonster->setPosition(ccp(TileXY.x,TileXY.y));
					pMonster->setAnchorPoint(ccp(0,0));
					m_pArrayMonsters->addObject(pMonster);
					addChild(pMonster,enZOrderFront);
				}
				if (strType->m_sString == "tortoise")
				{
					CMMonsterTortoise *pMonster = CMMonsterTortoise::CreateMonsterTortoise(TileXY,pMario,this,this);
					if (pMonster==NULL)
					{
						CCLog("pMonster==NULL!");
					}
					pMonster->setPosition(ccp(TileXY.x,TileXY.y));
					pMonster->setAnchorPoint(ccp(0,0));
					m_pArrayMonsters->addObject(pMonster);
					addChild(pMonster,enZOrderFront);
				}
				if (strType->m_sString == "flower")
				{
					CMMonsterFlower *pMonster = CMMonsterFlower::CreateMonsterFlower(TileXY,pMario,this,this);
					if (pMonster==NULL)
					{
						CCLog("pMonster==NULL!");
					}
					pMonster->setPosition(ccp(TileXY.x,TileXY.y));
					pMonster->setAnchorPoint(ccp(0,0));
					m_pArrayMonsters->addObject(pMonster);
					addChild(pMonster,enZOrderFront);
				}
			}
		}

CCTMXObjectGroup* pObjectLayer = objectGroupNamed("objects");这句代码从tmx地图里,获取objects层,从tiled工具看看,objects层有什么

objects层描述了这些怪物的位置


CCArray *ObjectArray = pObjectLayer->getObjects();这句代码,将怪物层的所有怪物精灵保存到ObjectArray的数组中

for (unsigned int i = 0; i < ObjectArray->count(); i++)  然后遍历所有的精灵

pDic = (CCDictionary *)ObjectArray->objectAtIndex(i); 获取下标i对应的精灵属性,返回的是一个字典,字典里保存着该精灵的所有属性

int PosX = ((CCString*)pDic->objectForKey("x"))->intValue();

int PosY = ((CCString*)pDic->objectForKey("y"))->intValue();

CCString *strName = (CCString*)pDic->objectForKey("name");

CCString *strType = (CCString*)pDic->objectForKey("type");

以上四句代码演示了,如何通过字典来获取精灵的属性


if (strType->m_sString == "mushroom")
                {
                    CMMonsterMushrooms *pMonster = CMMonsterMushrooms::CreateMonsterMushrooms(TileXY,pMario,this,this);
                    if (pMonster==NULL)
                    {
                        CCLog("pMonster==NULL!");
                    }
                    pMonster->setPosition(ccp(TileXY.x,TileXY.y));
                    pMonster->setAnchorPoint(ccp(0,0));
                    m_pArrayMonsters->addObject(pMonster);
                    addChild(pMonster,enZOrderFront);
                }

以上代码演示的是,如果这个怪物是蘑菇怪,那么创建蘑菇怪精灵,然后将蘑菇怪精灵设置好位置之后,加入到m_pArrayMonsters中。

将蘑菇怪保存到m_pArrayMonsters中,是为了马里奥移动时,要判断蘑菇怪和马里奥的碰撞检测。


砖头和金币的初始化,比怪物更加简单,这里不再叙述,大家可以自己看代码。


posted on 2014-02-13 10:29  三少爷的剑123  阅读(181)  评论(0编辑  收藏  举报

导航