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+lua[2]](/uploads/allimg/131030/110J64609-0-lp.jpg)








