[cocos2dx学习笔记]用cocos2dx3.X完成塔防游戏王国保卫战--地图(二)

前端之家收集整理的这篇文章主要介绍了[cocos2dx学习笔记]用cocos2dx3.X完成塔防游戏王国保卫战--地图(二)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

接上文,添加地图,在地图层直接添加即可,我是将AnchorPoint设置在(0,0),方便计算坐标

  1. mapSprite = Sprite::createWithSpriteFrameName(String::createWithFormat("Stage_%d.png",level+1)->getCString());
  2. mapSprite->setAnchorPoint(Point(0,0));
  3. mapSprite->setPosition(Point(0,0));
  4. addChild(mapSprite);

本章节主要介绍两个固定技能和商店技能的实现


首先是两个固定技能,以陨石为例

首先添加按键图片精灵

  1. stoneSprite = Sprite::createWithSpriteFrameName("power_portrait_fireball_0001.png");
  2. stoneSprite->setAnchorPoint(Point(0,0));
  3. stoneSprite->setPosition(Point(10,-20));
  4. stoneSprite->setName("inactive");
  5. //判断倒计时是否完毕
  6. completeStone = false;
  7. addChild(stoneSprite,1);

然后是倒计时遮盖层,采用的是ProgressTimer实现,放在按键图片精灵上面
  1. stoneTimer = ProgressTimer::create(Sprite::createWithSpriteFrameName("power_loading.png"));
  2. stoneTimer->setAnchorPoint(Point(0,0));
  3. //顺时针转动
  4. stoneTimer->setReverseDirection(true);
  5. stoneTimer->setPosition(Point(10,-20));
  6. stoneTimer->setPercentage(100);//显示原形的百分比
  7. this->addChild(stoneTimer,1);


添加定时器,更新ProgressTimer状态

  1. void PlayerStateMenu::updateStoneProgress(float Dt){
  2. stoneTimer->setPercentage(stoneTimer->getPercentage() - Dt*2);//更新进度2
  3. if (stoneTimer->getPercentage()==0) {
  4. this->unschedule(schedule_selector(PlayerStateMenu::updateStoneProgress));//取消定时器
  5. completeStone = true;
  6. }
  7. return;
  8. }

在你想要开始的时候schedule它比如第一波敌人出现之后
添加触摸响应
  1. auto stoneListener = EventListenerTouchOneByOne::create();
  2. stoneListener->onTouchBegan = [&](Touch* touch,Event* event){
  3. auto target = static_cast<Sprite*>(event->getCurrentTarget());
  4. Point locationInNode = target->convertTouchToNodeSpace(touch);
  5. Size size = target->getContentSize();
  6. Rect rect = Rect(0,size.width,size.height);
  7. //若第一次点击点击
  8. if(rect.containsPoint(locationInNode)){
  9. //若冷却结束
  10. if(completeStone == true){
  11. //移出其他技能触摸监听
  12. mTouchLayer->removeAllListener();
  13. if(stoneSprite->getName() == "inactive"){
  14. //设置为点击状态
  15. stoneSprite->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("power_portrait_fireball_0002.png"));
  16. //改变状态TAG
  17. stoneSprite->setName("active");
  18. //改变其他2个按键状态
  19. /****
  20. ****/
  21. //触摸层设置陨石技能监听
  22. mTouchLayer->setFireBallTouchShield();
  23. //第二次点击,即取消
  24. }else{
  25. mTouchLayer->removeFireBallTouchShield();
  26. stoneSprite->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("power_portrait_fireball_0001.png"));
  27. stoneSprite->setName("inactive");
  28. <span style="white-space:pre"> </span>}
  29. <span style="white-space:pre"> </span>}
  30. return true;
  31. }
  32. return false;
  33. };
  34. stoneListener->onTouchEnded = [&](Touch* touch,Event* event){
  35. };
  36. //触摸吞噬
  37. stoneListener->setSwallowTouches(true);
  38. _eventDispatcher->addEventListenerWithSceneGraPHPriority(stoneListener,stoneSprite);

当倒计时结束的时候,将completeStone置为true,只有此时点击按键才会触发。

点击技能,在触摸层添加一个EventListenerTouchOneByOne,覆盖整个触摸层,此时点击地图时,会执行这个触摸事件

下面来看看触摸层


  1. class TouchLayer :public Layer
  2. {
  3. public:
  4. virtual bool init();
  5. CREATE_FUNC(TouchLayer);
  6. EventListenerTouchOneByOne* touchlistener;
  7. EventListenerTouchOneByOne* FiereBalllistener;
  8.  
  9. void setFireBallTouchShield();
  10. void removeFireBallTouchShield();
  11. bool onFireBallTouchBegan(Touch* touch,Event* event);
  12. void onFireBallTouchEnded(Touch* touch,Event* event);
  13.  
  14. bool isFlag;
  15. bool onTouchBegan(Touch* touch,Event* event);
  16. void onTouchEnded(Touch* touch,Event* event);
  17. void onTouchMoved(Touch* touch,Event* event);
  18. Size winSize;
  19. bool isMoved;
  20.  
  21. void removeAllListener();
  22. };
这里我只截取了和陨石有关以及移动地图的部分

在BaseMap里添加触摸监听层

  1. void BaseMap::initTouchLayer()
  2. {
  3. mTouchLayer = TouchLayer::create();
  4. mTouchLayer->setContentSize(mapSprite->getContentSize());
  5. mTouchLayer->setAnchorPoint(Point(0,0));
  6. mTouchLayer->setPosition(Point(0,0));
  7. addChild(mTouchLayer,99);
  8. }

添加地图移动时间触摸

  1. touchlistener = EventListenerTouchOneByOne::create();
  2. touchlistener->onTouchBegan = CC_CALLBACK_2(TouchLayer::onTouchBegan,this);
  3. touchlistener->onTouchEnded = CC_CALLBACK_2(TouchLayer::onTouchEnded,this);
  4. touchlistener->onTouchMoved = CC_CALLBACK_2(TouchLayer::onTouchMoved,this);
  5. touchlistener->setSwallowTouches(true);
  6. _eventDispatcher->addEventListenerWithFixedPriority(touchlistener,-1);
  7. _eventDispatcher->addEventListenerWithSceneGraPHPriority(touchlistener,this);


这里设置将FiexPriority设置为-1为了确保触摸事件先与技能等其他触摸时间触发

  1. void TouchLayer::onTouchEnded(Touch* touch,Event* event)
  2. {
  3. <span style="white-space:pre"> </span>touchlistener->setSwallowTouches(isMoved);
  4. isMoved = false;
  5. }
  6.  
  7. void TouchLayer::onTouchMoved(Touch* touch,Event* event)
  8. {
  9. // 计算滑动过程中的滑动增量
  10. auto diff = touch->getDelta();
  11.  
  12. //手指移动修正,因为手指触摸不像鼠标触摸那么固定
  13. if(abs(diff.x) >5|| abs(diff.y) >5){
  14. isMoved = true;
  15. }
  16. // 得到当前bgSprite的位置
  17. auto currentPos = this->getParent()->getPosition();
  18. // 得到滑动后bgSprite应该所在的位置
  19. auto pos = currentPos + diff;
  20.  
  21. //边界控制,约束pos的位置
  22. pos.x = MIN(pos.x,0);
  23. pos.x = MAX(pos.x,-1200 + winSize.width);
  24. pos.y = MIN(pos.y,0);
  25. pos.y = MAX(pos.y,-1000 + winSize.height);
  26. // 重设地图层位置
  27.  
  28. this->getParent()->setPosition(pos);
  29. }

当手指在触摸层上移动的时候,isMoved会为true,这是setSwallowTouches(isMoved)会将其他触摸事件吞噬
这样是为了确保移动的时候经过或者移动结束的时候碰巧在某触摸点时,不会触发其他触摸事件(比如说移动完手指正好在某个防御塔上,这样就不会弹出防御塔升级层)
另外当移动的时候也不会触发技能事件监听,可以移动完再选择技能释放地点


  1. void TouchLayer::setFireBallTouchShield()
  2. {
  3. //调用方法创建陨石技能触摸时间
  4. FiereBalllistener = EventListenerTouchOneByOne::create();
  5. FiereBalllistener->onTouchBegan = CC_CALLBACK_2(TouchLayer::onFireBallTouchBegan,this);
  6. FiereBalllistener->onTouchEnded = CC_CALLBACK_2(TouchLayer::onFireBallTouchEnded,this);
  7. FiereBalllistener->setSwallowTouches(true);
  8. //设置比移动触摸事件高即可
  9. _eventDispatcher->addEventListenerWithFixedPriority(FiereBalllistener,1);
  10. _eventDispatcher->addEventListenerWithSceneGraPHPriority(FiereBalllistener,this);
  11. }
  12.  
  13. void TouchLayer::removeFireBallTouchShield()
  14. {
  15. //使用技能完毕去除此监听时间
  16. if(FiereBalllistener!=NULL)
  17. _eventDispatcher->removeEventListener(FiereBalllistener);
  18. }
  19.  
  20. bool TouchLayer::onFireBallTouchBegan(Touch* touch,Event* event)
  21. {
  22. //直接返回TRUE,拦截其他时间
  23. return true;
  24. }
  25.  
  26. void TouchLayer::onFireBallTouchEnded(Touch* touch,Event* event)
  27. {
  28. //播放音效
  29. SoundManager::playFireballUnleash();
  30. //创建3个陨石
  31. auto fireBall1 = FireBall::create();
  32. addChild(fireBall1);
  33. fireBall1->shoot(static_cast<TouchLayer*>(event->getCurrentTarget())->convertTouchToNodeSpace(touch)+Point(-30,300));
  34. auto fireBall2 = FireBall::create();
  35. addChild(fireBall2);
  36. fireBall2->shoot(static_cast<TouchLayer*>(event->getCurrentTarget())->convertTouchToNodeSpace(touch)+Point(0,350));
  37. auto fireBall3 = FireBall::create();
  38. addChild(fireBall3);
  39. fireBall3->shoot(static_cast<TouchLayer*>(event->getCurrentTarget())->convertTouchToNodeSpace(touch)+Point(30,280));
  40. //陨石坠落之后获取父类的玩家状态层,调用startStone,重新开始计时并且重置ProgressTimer遮盖层
  41. static_cast<BaseMap*>(this->getParent())->playerState->startStone();
  42. //移除此监听事件
  43. removeFireBallTouchShield();
  44. }

我实现整个技能监听加上触摸移动差不多就是这样

商店技能,召唤士兵等其他技能也是同样的思路,只是使用的技能不同罢了~其他的例如冰冻敌人、召唤士兵等将在对应的模块中一一介绍

今天先暂时写到这样,还有苦逼的实习等着我去找

猜你在找的Cocos2d-x相关文章