Cocos2d-x之塔防(二)让怪物跑起来

之前用的版本是2.0.1的版本,后来又用2.1.1重新写了一部分,现在全新开始,就用最新的版本,cocos2d-x-3.0beta,这个版本和之前的相比变化挺大的。

首先,肯定就是加载地图了,这里我用的是地图编辑器tIDE Tile Map Editor,具体用法请点击下面的链接

http://blog.csdn.net/nat_myron/article/details/8763570

这是用的背景的素材

Cocos2d-x之塔防(二)让怪物跑起来

程序实现方法:

    //===========加载背景地图=============
	auto bg = TMXTiledMap::create("bg.tmx");
	bg->setPosition(Point(0,0));
	this->addChild(bg,0);

这样就可以把我们编辑好的地图加载到场景中了,再修改一下bg.tmx文件,记录一下我们的路线。

<objectgroup name="path" width="60"height="60">
  <objectx="0" y="5"/>
  <objectx="2" y="5"/>
  <objectx="2" y="2"/>
  <objectx="4" y="2"/>
  <objectx="4" y="4"/>
  <objectx="7" y="4"/>
  <objectx="7" y="6"/>
  <objectx="11" y="6"/>
  <objectx="11" y="0"/>
</objectgroup>

程序中初始化路线的方法:

	auto map  = TMXTiledMap::create("bg.tmx");
	auto group = map->getObjectGroup("path");
	
	auto& objects = group->getObjects();
	for (auto& obj : objects)
	{
		ValueMap& dict = obj.asValueMap();

		int x = dict["x"].asInt();
		int y = 480 - dict["y"].asInt();
		wayPoint.push_back(Point(x,y));
		wayCount++;
	}

容器wayPoint记录tmx文件中的点。

好了,现在背景有了,路线也有了,接下来就是怪物了。

怪物的素材图

Cocos2d-x之塔防(二)让怪物跑起来

读取方法:

void Enemy::uploadEnemy()
{
	//RightMove()
	Vector<SpriteFrame*>tempRightMove;
	for (int i = 8; i < 12; i++)
	{
		tempRightMove.pushBack(Myframes[i]);
	}
	Animation* aniRightMove = Animation::createWithSpriteFrames(tempRightMove,0.2f);
	actionRightMove = Animate::create(aniRightMove);
	actionRightMove->retain();
	//LeftMove()
	Vector<SpriteFrame*>tempLeftMove;
	for (int i = 4; i < 8; i++)
	{
		tempLeftMove.pushBack(Myframes[i]);
	}
	Animation* aniLeftMove = Animation::createWithSpriteFrames(tempLeftMove,0.2f);
	actionLeftMove = Animate::create(aniLeftMove);
	actionLeftMove->retain();
	//UpMove()
	Vector<SpriteFrame*>tempUpMove;
	for (int i = 12; i < 16; i++)
	{
		tempUpMove.pushBack(Myframes[i]);
	}
	Animation* aniUpMove = Animation::createWithSpriteFrames(tempUpMove,0.2f);
	actionUpMove = Animate::create(aniUpMove);
	actionUpMove->retain();
	//DownMove()
	Vector<SpriteFrame*>tempDownMove;
	for (int i = 0; i < 4; i++)
	{
		tempDownMove.pushBack(Myframes[i]);
	}
	Animation* aniDownMove = Animation::createWithSpriteFrames(tempDownMove,0.2f);
	actionDownMove = Animate::create(aniDownMove);
	actionDownMove->retain();
}


创建怪物:

void GameScene::addEnemy()
{
	//============增加一个怪物==============
	auto enemy = Enemy::create();
	enemy->setPosition(Tools::ConvertViewPoint(DataManage::getInstance()->wayPoint[0]));
	this->addChild(enemy,1);
	DataManage::getInstance()->v_enemy.push_back(enemy);
}

怪物的移动:

void Enemy::run()
{
	if (this->iTurn< RoadCount - 1 && this->hp > 0)
	{
		Point point1 = DataManage::getInstance()->wayPoint[iTurn];
		Point point2 = DataManage::getInstance()->wayPoint[iTurn+1];

		Point p0 = Tools::ConvertViewPoint(point2);
		Point p = this->getPosition();
		//帧动画
		if(point2.x - point1.x == 0)//垂直
		{
			if(point2.y - point1.y >= 0 )
			{
				if (iChangeDire!=1)
				{
					this->stopAllActions();
					this->runAction(RepeatForever::create(actionDownMove));//1
					iChangeDire = 1;
				}
				p.y -= this->speed*1;
				if (p.y-p0.y<=0)
				{
					iTurn++;
					p = p0;
				}
			}
			else if(point2.y - point1.y < 0 )
			{
				if (iChangeDire!=2)
				{
					this->stopAllActions();
					this->runAction(RepeatForever::create(actionUpMove));//2
					iChangeDire = 2;
				}
				p.y += this->speed*1;
				if (p.y-p0.y>=0)
				{
					iTurn++;
					p = p0;
				}
			}
		}
		else if(point2.y - point1.y == 0)//水平
		{
			if(point2.x - point1.x > 0 )
			{
				if (iChangeDire!=3)
				{
					this->stopAllActions();
					this->runAction(RepeatForever::create(actionRightMove));//3
					iChangeDire = 3;
				}
				p.x += this->speed*1;
				if (p.x-p0.x>=0)
				{
					iTurn++;
					p = p0;
				}
			}
			else if(point2.x - point1.x <= 0 )
			{
				if (iChangeDire!=4)
				{
					this->stopAllActions();
					this->runAction(RepeatForever::create(actionLeftMove));//4
					iChangeDire = 4;
				}
				p.x -= this->speed*1;
				if (p.x-p0.x<=0)
				{
					iTurn++;
					p = p0;
				}
			}
		}
		this->setPosition(p);
	}
	//else if(this->iTurn >= RoadCount-1)
	//{
	//	this->removeFromParentAndCleanup(true);
	//	DataManage::getInstance()->v_enemy.erase(pos);
	//}
}

怪物出现的时间写在一个plist里

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>level1</key>  
    <dict>
	<key>1</key>
		<dict>
			<key>time</key>
			<string>0</string>
		</dict>
	<key>2</key>
		<dict>
			<key>time</key>
			<string>1</string>
		</dict>
	<key>3</key>
		<dict>
			<key>time</key>
			<string>2</string>
		</dict>
	<key>4</key>
		<dict>
			<key>time</key>
			<string>3</string>
		</dict>
	<key>5</key>
		<dict>
			<key>time</key>
			<string>4</string>
		</dict>
	<key>6</key>
		<dict>
			<key>time</key>
			<string>5</string>
		</dict>
	<key>7</key>
		<dict>
			<key>time</key>
			<string>6</string>
		</dict>
	</dict>
</dict>
<dict>

</dict>
</plist>

读取方法:

	__Dictionary* plistDic = __Dictionary::createWithContentsOfFile("level1.plist");
	__Dictionary* levelDic = dynamic_cast<__Dictionary*>(plistDic->objectForKey("level1"));
	char str[10];
	for (int i=1;i<=levelDic->count();i++)
	{
		sprintf_s(str,"%d",i);
		__Dictionary* farScene = dynamic_cast<__Dictionary*>(levelDic->objectForKey(str));
		__String* spriteName = dynamic_cast<__String*>(farScene->objectForKey("time")); 
		float time = spriteName->floatValue();
		v_time.push_back(time);
	}

主场景开启一个定时器,遍历所有的怪物,让怪物出现并跑起来

void GameScene::update( float dt )
{
	//==========enemy run================
	vector<Enemy*>::iterator it=DataManage::getInstance()->v_enemy.begin();
	while(it!=DataManage::getInstance()->v_enemy.end())
	{
		if((*it)->iTurn >= (*it)->RoadCount-1)
		{
			(*it)->removeFromParentAndCleanup(true);
			it = DataManage::getInstance()->v_enemy.erase(it);
		}else
		{
			(*it)->run();
			it++;
		}
	}
	//========enemy show time===================
	vector<float>::iterator it_time=DataManage::getInstance()->v_time.begin();
	while(it_time!=DataManage::getInstance()->v_time.end())
	{
		float tmp = fabs(time-*it_time);
		if(( tmp>-0.0001) && (tmp < 0.0001))
		{
			addEnemy();
			it_time = DataManage::getInstance()->v_time.erase(it_time);
		}else
		{
			it_time++;
		}
	}
	time += 1.0/60;
}
坐标转换函数

Point Tools::ConvertViewPoint( Point p )
{
	return Director::getInstance()->convertToGL(CCPoint(p.x * 60 + 30, p.y * 60));
}

看一下运行图

Cocos2d-x之塔防(二)让怪物跑起来

这一篇就到此为止了。





 

Cocos2d-x之塔防(二)让怪物跑起来

上一篇:Count and Say


下一篇:老生长谈,使用spring发送mail