![[cocos2dx学习笔记]用cocos2dx3.X完成塔防游戏王国保卫战--地图(二),第1张 [cocos2dx学习笔记]用cocos2dx3.X完成塔防游戏王国保卫战--地图(二),第1张](/aiimages/%5Bcocos2dx%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%5D%E7%94%A8cocos2dx3.X%E5%AE%8C%E6%88%90%E5%A1%94%E9%98%B2%E6%B8%B8%E6%88%8F%E7%8E%8B%E5%9B%BD%E4%BF%9D%E5%8D%AB%E6%88%98--%E5%9C%B0%E5%9B%BE%EF%BC%88%E4%BA%8C%EF%BC%89.png)
接上文,添加地图,在地图层直接添加即可,我是将AnchorPoint设置在(0,0),方便计算坐标
mapSprite = Sprite::createWithSpriteFramename(String::createWithFormat("Stage_%d.png",level+1)->getCString());mapSprite->setAnchorPoint(Point(0,0));mapSprite->setposition(Point(0,0));addChild(mapSprite); 本章节主要介绍两个固定技能和商店技能的实现
首先是两个固定技能,以陨石为例
首先添加按键图片精灵
stonesprite = Sprite::createWithSpriteFramename("power_portrait_fireball_0001.png");stonesprite->setAnchorPoint(Point(0,0));stonesprite->setposition(Point(10,-20));stonesprite->setname("inactive");//判断倒计时是否完毕completeStone = false;addChild(stonesprite,1); 然后是倒计时遮盖层,采用的是Progresstimer实现,放在按键图片精灵上面
stoneTimer = Progresstimer::create(Sprite::createWithSpriteFramename("power_loading.png"));stoneTimer->setAnchorPoint(Point(0,0));//顺时针转动stoneTimer->setReverseDirection(true);stoneTimer->setposition(Point(10,-20));stoneTimer->setPercentage(100);//显示原形的百分比this->addChild(stoneTimer,1);
添加定时器,更新Progresstimer状态
voID PlayerStateMenu::updateStoneProgress(float Dt){ stoneTimer->setPercentage(stoneTimer->getPercentage() - Dt*2);//更新进度2 if (stoneTimer->getPercentage()==0) { this->unschedule(schedule_selector(PlayerStateMenu::updateStoneProgress));//取消定时器 completeStone = true; } return;} 在你想要开始的时候schedule它比如第一波敌人出现之后
添加触摸响应
auto stoneListener = EventListenertouchOneByOne::create();stoneListener->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)){ //若冷却结束 if(completeStone == true){ //移出其他技能触摸监听 mtouchLayer->removeAllListener(); if(stonesprite->getname() == "inactive"){ //设置为点击状态 stonesprite->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("power_portrait_fireball_0002.png")); //改变状态TAG stonesprite->setname("active"); //改变其他2个按键状态 /**** ****/ //触摸层设置陨石技能监听 mtouchLayer->setFireBalltouchShIEld(); //第二次点击,即取消 }else{ mtouchLayer->removeFireBalltouchShIEld(); stonesprite->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("power_portrait_fireball_0001.png")); stonesprite->setname("inactive"); <span > </span>}<span > </span>} return true; } return false; };stoneListener->ontouchended = [&](touch* touch,Event* event){};//触摸吞噬stoneListener->setSwallowtouches(true);_eventdispatcher->addEventListenerWithSceneGraPHPriority(stoneListener,stonesprite); 当倒计时结束的时候,将completeStone置为true,只有此时点击按键才会触发。
点击技能,在触摸层添加一个EventListenertouchOneByOne,覆盖整个触摸层,此时点击地图时,会执行这个触摸事件
下面来看看触摸层
class touchLayer :public Layer{public: virtual bool init(); CREATE_FUNC(touchLayer); EventListenertouchOneByOne* touchListener; EventListenertouchOneByOne* FIEreBallListener; voID setFireBalltouchShIEld(); voID removeFireBalltouchShIEld(); bool onFireBalltouchBegan(touch* touch,Event* event); voID onFireBalltouchended(touch* touch,Event* event); bool isFlag; bool ontouchBegan(touch* touch,Event* event); voID ontouchended(touch* touch,Event* event); voID ontouchmoved(touch* touch,Event* event); Size winSize; bool isMoved; voID removeAllListener();};这里我只截取了和陨石有关以及移动地图的部分 在BaseMap里添加触摸监听层
voID BaseMap::inittouchLayer(){ mtouchLayer = touchLayer::create(); mtouchLayer->setContentSize(mapSprite->getContentSize()); mtouchLayer->setAnchorPoint(Point(0,0)); mtouchLayer->setposition(Point(0,0)); addChild(mtouchLayer,99);} 添加地图移动时间触摸
touchListener = EventListenertouchOneByOne::create();touchListener->ontouchBegan = CC_CALLBACK_2(touchLayer::ontouchBegan,this);touchListener->ontouchended = CC_CALLBACK_2(touchLayer::ontouchended,this);touchListener->ontouchmoved = CC_CALLBACK_2(touchLayer::ontouchmoved,this);touchListener->setSwallowtouches(true);_eventdispatcher->addEventListenerWithFixedPriority(touchListener,-1);_eventdispatcher->addEventListenerWithSceneGraPHPriority(touchListener,this);
voID touchLayer::ontouchended(touch* touch,Event* event){<span > </span>touchListener->setSwallowtouches(isMoved); isMoved = false;}voID touchLayer::ontouchmoved(touch* touch,Event* event){ // 计算滑动过程中的滑动增量 auto diff = touch->getDelta(); //手指移动修正,因为手指触摸不像鼠标触摸那么固定 if(abs(diff.x) >5|| abs(diff.y) >5){ isMoved = true; } // 得到当前bgSprite的位置 auto currentPos = this->getParent()->getposition(); // 得到滑动后bgSprite应该所在的位置 auto pos = currentPos + diff; //边界控制,约束pos的位置 pos.x = MIN(pos.x,0); pos.x = MAX(pos.x,-1200 + winSize.wIDth); pos.y = MIN(pos.y,0); pos.y = MAX(pos.y,-1000 + winSize.height); // 重设地图层位置 this->getParent()->setposition(pos);} 当手指在触摸层上移动的时候,isMoved会为true,这是setSwallowtouches(isMoved)会将其他触摸事件吞噬
这样是为了确保移动的时候经过或者移动结束的时候碰巧在某触摸点时,不会触发其他触摸事件(比如说移动完手指正好在某个防御塔上,这样就不会d出防御塔升级层)
另外当移动的时候也不会触发技能事件监听,可以移动完再选择技能释放地点
voID touchLayer::setFireBalltouchShIEld(){ //调用此方法创建陨石技能触摸时间 FIEreBallListener = EventListenertouchOneByOne::create(); FIEreBallListener->ontouchBegan = CC_CALLBACK_2(touchLayer::onFireBalltouchBegan,this); FIEreBallListener->ontouchended = CC_CALLBACK_2(touchLayer::onFireBalltouchended,this); FIEreBallListener->setSwallowtouches(true); //设置比移动触摸事件高即可 _eventdispatcher->addEventListenerWithFixedPriority(FIEreBallListener,1); _eventdispatcher->addEventListenerWithSceneGraPHPriority(FIEreBallListener,this);}voID touchLayer::removeFireBalltouchShIEld(){ //使用技能完毕去除此监听时间 if(FIEreBallListener!=NulL) _eventdispatcher->removeEventListener(FIEreBallListener);}bool touchLayer::onFireBalltouchBegan(touch* touch,Event* event){ //直接返回TRUE,拦截其他时间 return true;}voID touchLayer::onFireBalltouchended(touch* touch,Event* event){ //播放音效 SoundManager::playFireballUnleash(); //创建3个陨石 auto fireBall1 = FireBall::create(); addChild(fireBall1); fireBall1->shoot(static_cast<touchLayer*>(event->getCurrentTarget())->converttouchToNodeSpace(touch)+Point(-30,300)); auto fireBall2 = FireBall::create(); addChild(fireBall2); fireBall2->shoot(static_cast<touchLayer*>(event->getCurrentTarget())->converttouchToNodeSpace(touch)+Point(0,350)); auto fireBall3 = FireBall::create(); addChild(fireBall3); fireBall3->shoot(static_cast<touchLayer*>(event->getCurrentTarget())->converttouchToNodeSpace(touch)+Point(30,280)); //陨石坠落之后获取父类的玩家状态层,调用startStone,重新开始计时并且重置Progresstimer遮盖层 static_cast<BaseMaP*>(this->getParent())->playerState->startStone(); //移除此监听事件 removeFireBalltouchShIEld();} 我实现整个技能监听加上触摸移动差不多就是这样
商店技能,召唤士兵等其他技能也是同样的思路,只是使用的技能不同罢了~其他的例如冰冻敌人、召唤士兵等将在对应的模块中一一介绍
今天先暂时写到这样,还有苦逼的实习等着我去找
总结以上是内存溢出为你收集整理的[cocos2dx学习笔记]用cocos2dx3.X完成塔防游戏王国保卫战--地图(二)全部内容,希望文章能够帮你解决[cocos2dx学习笔记]用cocos2dx3.X完成塔防游戏王国保卫战--地图(二)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)