该张补充如何建造防御塔
首先是,点击该图标,会d出上面那张GIF的建造选项。
具体代码在Terrain(名字是根据资源包图片名起的)中,首先根据每一个地图算好坐标,在地图添加到场景后执行addTerrains()添加到地图层中。在Terrain的Init函数中根据地图类型选择图片生成精灵,并添加触摸监听。
auto Listener = EventListenertouchOneByOne::create();
Listener->ontouchBegan = CC_CALLBACK_2(Terrain::ontouchBegan,this);
Listener->ontouchended = CC_CALLBACK_2(Terrain::ontouchended,this);
_eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,terrain);
在ontouchended中判断
voID Terrain::ontouchended(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)&&target->isVisible())//若点击在该区域,且可见(可见是因为若防御塔建造了,就不可以了,就不会执行下面代码) { if(isUpdateMenuShown)//如果已经显示,则隐藏 { hIDeUpdateMenu(); }else{ showUpdateMenu(); } }else{//若点击其他区域,隐藏 hIDeUpdateMenu(); }}
在showUpdateMenu()添加建造菜单层
voID Terrain::showUpdateMenu(){ auto towerPanleLayer = TowerPanleLayer::create(); towerPanleLayer->setposition(this->getposition()); towerPanleLayer->setTag(getTag()); towerPanleLayer->setMyTerrain(this); static_cast<BaseMaP*>(this->getParent())->mtouchLayer->addChild(towerPanleLayer); towerPanleLayer->inAnimation(); isUpdateMenuShown = true;}
TowerPanleLayer是自定义Layer
bool TowerPanleLayer::init(){ if ( !Sprite::init() ) { return false; } circle = Circle::create(); addChild(circle); circle->setVisible(false); planesprite = Sprite::createWithSpriteFramename("gui_ring.png"); planesprite->setposition(Point(0,0)); planesprite->setScale(0.5f); addChild(planesprite); isBuilt = false; return true;}其中Circle是自定义精灵,根据射程显示防御塔攻击范围,不做详细介绍了,planesprite是那个铁环
首先通过this->getposition()获取位置,将它添加在触摸层中,执行从小变大的动画。
voID TowerPanleLayer::inAnimation(){ SoundManager::playTowerMenu(); planesprite->runAction(Sequence::create(Scaleto::create(0.1f,1.0f,1.0f),CallFuncN::create(CC_CALLBACK_0(TowerPanleLayer::addIcons,this)),NulL)); }动画执行完毕后,添加4个按键,4个按键是自定义精灵类,具体参见代码,给4个按键添加触摸监听,在触摸开始时:
bool TowerPanleLayer::ontouchBegan(touch *touch,Event *event){ auto target = static_cast<BaseBuildIcon*>(event->getCurrentTarget()); Point locationInNode = target->converttouchToNodeSpace(touch); Size size = target->baseIcon->getContentSize(); Rect rect = Rect(0-size.wIDth/2,0-size.height/2,size.height); if (rect.containsPoint(locationInNode)) { if(target->getIsSelected())//如果已经选中(第二次按下) { if(target->getIsAble())//如果钱够用 { int i = target->getType(); addTower(target->getType()); } }else{//第一次按下 archerIcon->setNotSelected(); artilleryIcon->setNotSelected(); barracksIcon->setNotSelected(); magicIcon->setNotSelected(); target->setSelected();//设置为选中状态 addTempTower(target->getType()); } return true; } return false;}
其中addTempTower()是添加临时防御塔,即第一张图的效果,当点击第二次,通过addTower()建造防御塔
voID TowerPanleLayer::addTempTower(int type){ if(tempTower!=NulL){ removeChild(tempTower,true);//首先上一个临时塔移除 } circle->setVisible(true);//显示防御塔攻击范围 switch (type) { case(1):{//根据点击类型显示 tempTower = Sprite::createWithSpriteFramename("tower_prevIEw_archer.png"); tempTower->setposition(Point(0,25)); static_cast<BaseMaP*>(this->getParent()->getParent())->playerState->showTowerInfo(ARCHER_1);}//在菜单层显示防御塔信息 break; ..........}addTower中建造防御塔
voID TowerPanleLayer::addTower(int type){ static_cast<BaseMaP*>(this->getParent()->getParent())->playerState->removetowerInfo();//移除防御塔信息 switch (type) { case(2):{//根据选择类型建造 auto barracksTower = BasebarracksTower::create(); barracksTower->setposition(Point(0,20)); barracksTower->setTag(terrain->getTag()); barracksTower->setMyTerrain(terrain);//设置塔所属的terrain,防御塔售出时将此设置可见。 terrain->addChild(barracksTower); barracksTower->buildingAnimation(); GameManager::getInstance()->MONEY = GameManager::getInstance()->MONEY - barracksTower->getBuildMoney(); } break; .........省略 } SoundManager::playTowerBuilding();//播放建造音效 isBuilt = true; terrain->terrain->setVisible(false);//将terrain设置不可见,再点击即不会再显示了 this->setVisible(false);}另外,防御塔升级的界面类似建造,就不一一介绍了,升级即将上一个塔remove掉,新建一个新的。卖塔的过程即将防御塔remove掉,并且将terrain设置为可见 总结
以上是内存溢出为你收集整理的[cocos2dx学习笔记]用cocos2dx3.X完成塔防游戏王国保卫战--防御塔(四)全部内容,希望文章能够帮你解决[cocos2dx学习笔记]用cocos2dx3.X完成塔防游戏王国保卫战--防御塔(四)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)