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

来源:未知 责任编辑:责任编辑 发表时间:2014-01-26 21:59 点击:

创建怪物:

 

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里

 

 





level1  
    
	1
		
			time
			0
		
	2
		
			time
			1
		
	3
		
			time
			2
		
	4
		
			time
			3
		
	5
		
			time
			4
		
	6
		
			time
			5
		
	7
		
			time
			6
		
	






读取方法:

 

 

	__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);
	}
	
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • cocos2d-x学习笔记(19)--label 、label atlas
  • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
  • Cocos2d-x学习(一):HelloWorld
  • cocos2dx在xcode下开发,编译到android上(2)
  • cocos2d 设置屏幕默认方向
  • Cocos2d-x 2.0 之 Actions “三板斧” 之一
  • cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoi
  • cocos2d-x学习笔记(18)--游戏打包(windows平台)
  • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1