兵营塔可以产生三个士兵,可以设置士兵集结点,当周围有敌人通过时,士兵会拦下敌人,并与敌人进行搏斗。每隔N秒,会回复士兵血量,可以升级,有更多的兵种。
去其他防御塔相比,兵营塔在建造完毕,即上一章节中的buildingSmokeAnimation执行完毕后创建3个士兵,给士兵设置集结点,由士兵自行走到集结点。
setRallyPoint是重新设置集结点,士兵会停止一切动作,走到新的集结点(runTolocation()方法)。
voID BasebarracksTower::setRallyPoint(Point point){ auto rally = point - this->getParent()->getposition(); SoldIErs.at(0)->setLocation(Point(rally.x + 20,rally.y - 20)); SoldIErs.at(1)->setLocation(Point(rally.x - 20,rally.y - 20)); SoldIErs.at(2)->setLocation(Point(rally.x,rally.y + 20)); for(int i = 0; i< SoldIErs.size()&& SoldIErs.at(i)!=NulL && SoldIErs.at(i)->getState()!=SoldIErStateDeath;i++) { if(SoldIErs.at(i)->nearestMonster!=NulL && SoldIErs.at(i)->nearestMonster->getState()!=stateDeath) SoldIErs.at(i)->nearestMonster->restartWalking(); SoldIErs.at(i)->runTolocation(SoldIErs.at(i)->getLocation()); }}
cureSoldIErs(float dt)是设置在每隔dt时间,对士兵进行一次治疗,若有死掉的士兵,自动制造一名新的士兵。
voID BasebarracksTower::cureSoldIErs(float dt){ for(int i = 0;i<SoldIErs.size();i++)//遍历士兵 { auto SoldIEr = SoldIErs.at(i); if(SoldIEr->getState() == SoldIErStateWait){//若士兵没有处于攻击状态 SoldIEr->setCurrHp(SoldIEr->getMaxHp());//回复士兵所有血量 SoldIEr->setHpPercentage(100); SoldIEr->getHpbar()->setPercentage(100); } else if(SoldIEr->getState() == SoldIErStateDeath){//若有士兵死亡 if(level <= 3){ SoundManager::playbarrackOpendoor();//播放兵营开门声音以及动画 door->runAction(Animate::create(AnimationCache::getInstance()->getAnimation(String::createWithFormat("level%d_barracks_ope ndoor",getLevel())->getCString()))); auto point = SoldIEr->getLocation(); SoldIEr ->removeFromParent(); SoldIEr = NulL;//将死亡的士兵清除 auto newSoldIEr = barrackSoldIEr::createSoldIEr(point,level);//创建新的士兵 SoldIErs.replace(i,newSoldIEr);//更新士兵队列 addChild(newSoldIEr); }//省略。。。。。 }}
士兵塔主要工作都放在士兵类当中,士兵塔只负责根据士兵的状态更新士兵
士兵塔的升级界面略有不同,多了一个旗帜用于设置集结点,代码部分在UpdatePanleLayerWithFlag中
auto flagListener = EventListenertouchOneByOne::create(); flagListener->ontouchBegan = [&](touch* touch,Event* event){ auto target = static_cast<Sprite*>(event->getCurrentTarget()); Point locationInNode = target->converttouchToNodeSpace(touch); Size size = target->getContentSize(); Rect rect = Rect(0,size.wIDth,size.height); if (rect.containsPoint(locationInNode)) { return true; } return false; }; flagListener->ontouchended = [&](touch* touch,Event* event){ static_cast<touchLayer*>(this->getParent())->tower = this->getTower(); static_cast<touchLayer*>(this->getParent())->setRallyFlagtouchShIEld(); tower->isUpdateMenuShown = false; this->removeFromParent(); };
当点击旗帜后,清除升级菜单,通知touchLayer显示集结点范围,并且创建监听事件
voID touchLayer::setRallyFlagtouchShIEld(){ rallyCircle = RallyCircle::create(); rallyCircle->setposition(tower->getParent()->getposition()); rallyCircle->setTag(1001); addChild(rallyCircle); Listener = EventListenertouchOneByOne::create(); Listener->ontouchBegan = CC_CALLBACK_2(touchLayer::onRallyFlagtouchBegan,this); Listener->ontouchended = CC_CALLBACK_2(touchLayer::onRallyFlagtouchended,this); Listener->setSwallowtouches(true); _eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,this);}当点击在范围内,显示集结点旗帜,重设士兵塔的集结点即setRallyPoint方法
bool touchLayer::onRallyFlagtouchBegan(touch* touch,Event* event){ auto target = static_cast<touchLayer*>(event->getCurrentTarget()); Point locationInNode = target->converttouchToNodeSpace(touch); if(locationInNode.distance(tower->getParent()->getposition())<160 && locationInNode.distance(tower->getParent()->getposition())>80){ addRallyFlag(locationInNode); tower->setRallyPoint(locationInNode); SoundManager::playRallyPointPlaced(); isFlag = true; }else{ adDWrongPlace(static_cast<touchLayer*>(event->getCurrentTarget())->converttouchToNodeSpace(touch)); } return true;}以上即士兵塔基本实现方法。 总结
以上是内存溢出为你收集整理的[cocos2dx学习笔记]用cocos2dx3.X完成塔防游戏王国保卫战--防御塔(六)之兵营塔全部内容,希望文章能够帮你解决[cocos2dx学习笔记]用cocos2dx3.X完成塔防游戏王国保卫战--防御塔(六)之兵营塔所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)